我的代码:我的ostream和istream的友元函数不能访问私有类成员
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;
}
为主。 cpp
#include "matrix.h"
#include <iostream>
using namespace std;
int main()
{
Matrix a;
cout << "Matrix a: " << a << endl;
system("pause");
return 0;
}
错误:
- 构件 “矩阵行::”(在第3行matrix.h声明“)是不可访问
- 构件 ”矩阵::栏“(在第3行matrix.h声明“)是不可访问
- 构件‘矩阵阵列::’(在第3行matrix.h声明”)是不可访问
- 二进制“< <”:没有操作员发现它采用类型“矩阵”的右边的操作数
- '的ostream':不明确的符号
- '的IStream':不明确的符号
我在做什么错? :(
**编辑:。我editted问题给予MCVE例如像巴里建议,并且也去掉using namespace std
像斯拉瓦建议我仍然得到同样的错误
请提供[最小,**完整* *和可验证的示例](http://www.stackoverflow.com/help/mcve)。 Matrix是一个命名空间吗? – Barry
我发布的是我的确切代码。我唯一遗漏的部分是使用“...”的其他函数@Barry – ChewingSomething
您的矩阵类不包含''?或者关闭'};'? –
Barry