【发布时间】:2015-09-25 15:16:45
【问题描述】:
我的代码:
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 声明)不可访问
- 二进制“
- 'ostream':不明确的符号
- 'istream':模棱两可的符号
我做错了什么? :(
**已编辑:我已编辑问题以提供像 Barry 建议的 MCVE 示例,并像 Slava 推荐的那样删除了 using namespace std。我仍然遇到同样的错误。
【问题讨论】:
-
请提供minimal, complete, and verifiable example。
Matrix是命名空间吗? -
我发布的是我的确切代码。我唯一遗漏的部分是使用“...”@Barry 的其他功能
-
您的矩阵类不包括
<iostream>?或者有一个结束};? -
哦,确实有 };但不,它不包括
。这就是它不起作用的问题吗? @巴里 -
请查看 MCVE 链接并发布完整示例。如果没有完整且可验证的示例,就不可能知道问题所在。
标签: c++ matrix friend-function