我的 ostream 和 istream 好友功能无法访问私人 class 成员
My ostream and istream friend function can't access private class members
我的代码:
matrix.h
#include <iostream>
class Matrix {
private:
int row;
int col;
int **array;
public:
Matrix();
friend std::ostream& operator<<(ostream& output, const Matrix& m);
};
matrix.cpp
#include "matrix.h"
#include <iostream>
Matrix::Matrix()
{
row = 3;
col = 4;
array = new int*[row];
for (int i = 0; i < row; ++i)
{
array[i] = new int[col];
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
array[i][j] = 0;
}
}
}
std::ostream& operator<<(std::ostream& output, const Matrix& m)
{
output << "\nDisplay elements of Matrix: " << std::endl;
for (int i = 0; i < m.row; ++i)
{
for (int j = 0; j < m.col; ++j)
{
output << m.array[i][j] << " ";
}
output << std::endl;
}
return output;
}
main.cpp
#include "matrix.h"
#include <iostream>
using namespace std;
int main()
{
Matrix a;
cout << "Matrix a: " << a << endl;
system("pause");
return 0;
}
错误:
- 成员 "Matrix::row"(在第 3 行声明 matrix.h")不可访问
- 成员 "Matrix::col"(在第 3 行声明 matrix.h")不可访问
- 成员 "Matrix::array"(在第 3 行声明 matrix.h")不可访问
- 二进制“<<”: 没有找到接受 'Matrix'
类型右操作数的运算符
- 'ostream': 不明确的符号
- 'istream': 不明确的符号
我做错了什么? :(
** 已编辑:我已经编辑了问题以给出 Barry 建议的 MCVE 示例,并且还删除了 using namespace std
,如 Slava 所建议的。我仍然遇到同样的错误。
What am I doing wrong? :(
将语句 using namespace std;
放入 header 是一种不好的做法,这是有原因的。据此:
'ostream': ambiguous symbol
'istream': ambiguous symbol
您在全局名称空间的某处声明了 istream 和 ostream。按照良好的习惯,输入 std::
并不难
现在您已经有了一个完整的示例,您在这里缺少 std::
:
friend std::ostream& operator<<(ostream& output, const Matrix& m);
^^^
添加它,一切都可以正常编译:
friend std::ostream& operator<<(std::ostream& output, const Matrix& m);
我的代码:
matrix.h
#include <iostream>
class Matrix {
private:
int row;
int col;
int **array;
public:
Matrix();
friend std::ostream& operator<<(ostream& output, const Matrix& m);
};
matrix.cpp
#include "matrix.h"
#include <iostream>
Matrix::Matrix()
{
row = 3;
col = 4;
array = new int*[row];
for (int i = 0; i < row; ++i)
{
array[i] = new int[col];
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
array[i][j] = 0;
}
}
}
std::ostream& operator<<(std::ostream& output, const Matrix& m)
{
output << "\nDisplay elements of Matrix: " << std::endl;
for (int i = 0; i < m.row; ++i)
{
for (int j = 0; j < m.col; ++j)
{
output << m.array[i][j] << " ";
}
output << std::endl;
}
return output;
}
main.cpp
#include "matrix.h"
#include <iostream>
using namespace std;
int main()
{
Matrix a;
cout << "Matrix a: " << a << endl;
system("pause");
return 0;
}
错误:
- 成员 "Matrix::row"(在第 3 行声明 matrix.h")不可访问
- 成员 "Matrix::col"(在第 3 行声明 matrix.h")不可访问
- 成员 "Matrix::array"(在第 3 行声明 matrix.h")不可访问
- 二进制“<<”: 没有找到接受 'Matrix' 类型右操作数的运算符
- 'ostream': 不明确的符号
- 'istream': 不明确的符号
我做错了什么? :(
** 已编辑:我已经编辑了问题以给出 Barry 建议的 MCVE 示例,并且还删除了 using namespace std
,如 Slava 所建议的。我仍然遇到同样的错误。
What am I doing wrong? :(
将语句 using namespace std;
放入 header 是一种不好的做法,这是有原因的。据此:
'ostream': ambiguous symbol 'istream': ambiguous symbol
您在全局名称空间的某处声明了 istream 和 ostream。按照良好的习惯,输入 std::
现在您已经有了一个完整的示例,您在这里缺少 std::
:
friend std::ostream& operator<<(ostream& output, const Matrix& m);
^^^
添加它,一切都可以正常编译:
friend std::ostream& operator<<(std::ostream& output, const Matrix& m);