289 6 D:[Error] no match for operator<< (operand types are std::ostream {aka std::basic_ostream<char> } and Matrix<double> )
时间: 2023-06-27 17:07:18 浏览: 86
这个错误提示是因为你想要使用 `operator<<` 将 `Matrix<double>` 类型的对象输出到标准输出流,但是该类型并没有重载该运算符。
你可以自己实现该运算符的重载。以下是一个示例:
```c++
#include <iostream>
#include <vector>
class Matrix {
public:
Matrix(int rows, int cols) : rows_(rows), cols_(cols), data_(rows * cols) {}
double& operator()(int row, int col) {
return data_[row * cols_ + col];
}
double operator()(int row, int col) const {
return data_[row * cols_ + col];
}
int rows() const {
return rows_;
}
int cols() const {
return cols_;
}
private:
int rows_;
int cols_;
std::vector<double> data_;
};
std::ostream& operator<<(std::ostream& os, const Matrix& matrix) {
for (int i = 0; i < matrix.rows(); ++i) {
for (int j = 0; j < matrix.cols(); ++j) {
os << matrix(i, j) << " ";
}
os << std::endl;
}
return os;
}
int main() {
Matrix mat(2, 3);
mat(0, 0) = 1.0;
mat(0, 1) = 2.0;
mat(0, 2) = 3.0;
mat(1, 0) = 4.0;
mat(1, 1) = 5.0;
mat(1, 2) = 6.0;
std::cout << mat << std::endl;
return 0;
}
```
运行结果如下:
```
1 2 3
4 5 6
```