C++ 中的运算符 <<,"no operator found which takes right hand-operator" 和 "already has a body" 错误
Operator << in C++, "no operator found which takes right hand-operator" and "already has a body" error
我有一些代码应该重载运算符 << 并能够打印矩阵对象,但由于某种原因,它失败了。这是它的代码:(Matirx.h 文件)
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <int N=1, int M=1, class T = int>
class Matrix {
int rows;
int cols;
T matrix[N][M];
public:
Matrix(T matrixVal = 0) :rows(N), cols(M) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this->matrix[i][j] = matrixVal;
}
}
}
int getRows() { return rows; }
int getCols() { return cols; }
T** getMatrix() { return matrix; }
friend ostream& operator<<(ostream& out, const Matrix<>& mat) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
out << this->matrix[i][j];
out << " ";
}
out << endl;
}
return out;
}
但是当我添加它时,出现以下错误:
Error C2084 function 'std::ostream &Matrix<4,4,int>::operator <<(std::ostream &,const Matrix<1,1,int> &)' already has a body
Error C2679 binary '<<': no operator found which takes a right-hand
operand of type 'Matrix<4,4,int>' (or there is no acceptable conversion)
Error (active) E0349 no operator "<<" matches these operands
这是主要代码:
#include "matrix.h"
template <int row, int col, typename T>
void printDiag(Matrix<row, col, T>& mat) {
int number;
T* diag = mat.getDiag(number);
for (int i = 0; i < number; i++)
{
std::cout << diag[i] << " ";
}
std::cout << std::endl;
delete[] diag;
}
int main() {
//freopen("output_matrix.txt", "w", stdout);
Matrix<4, 4> mat;
std::cout << mat << std::endl;
Matrix<4, 4> identity(1);
std::cout << identity << std::endl;
如有任何帮助,我们将不胜感激 (:
这有几个问题。首先在声明中您必须指定模板参数。由于整个 class 是模板化的,您不需要在之前显式添加其他 template
,但您需要 Matrix<N,M,T>
.
第二个问题是您将 friend std::ostream& operator<<(...)
视为成员函数(使用成员变量和 this
)。不是这种情况。您可能在 class 中声明它,但它绝对不是 class 的一部分。在使用 this->
的地方使用 mat.
,在只使用 rows
/cols
的地方使用 mat.rows
/mat.cols
.
我也不得不稍微修改一下构造函数,但我仍然不知道为什么它会抱怨。
我有一些代码应该重载运算符 << 并能够打印矩阵对象,但由于某种原因,它失败了。这是它的代码:(Matirx.h 文件)
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <int N=1, int M=1, class T = int>
class Matrix {
int rows;
int cols;
T matrix[N][M];
public:
Matrix(T matrixVal = 0) :rows(N), cols(M) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this->matrix[i][j] = matrixVal;
}
}
}
int getRows() { return rows; }
int getCols() { return cols; }
T** getMatrix() { return matrix; }
friend ostream& operator<<(ostream& out, const Matrix<>& mat) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
out << this->matrix[i][j];
out << " ";
}
out << endl;
}
return out;
}
但是当我添加它时,出现以下错误:
Error C2084 function 'std::ostream &Matrix<4,4,int>::operator <<(std::ostream &,const Matrix<1,1,int> &)' already has a body
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'Matrix<4,4,int>' (or there is no acceptable conversion)
Error (active) E0349 no operator "<<" matches these operands
这是主要代码:
#include "matrix.h"
template <int row, int col, typename T>
void printDiag(Matrix<row, col, T>& mat) {
int number;
T* diag = mat.getDiag(number);
for (int i = 0; i < number; i++)
{
std::cout << diag[i] << " ";
}
std::cout << std::endl;
delete[] diag;
}
int main() {
//freopen("output_matrix.txt", "w", stdout);
Matrix<4, 4> mat;
std::cout << mat << std::endl;
Matrix<4, 4> identity(1);
std::cout << identity << std::endl;
如有任何帮助,我们将不胜感激 (:
这有几个问题。首先在声明中您必须指定模板参数。由于整个 class 是模板化的,您不需要在之前显式添加其他 template
,但您需要 Matrix<N,M,T>
.
第二个问题是您将 friend std::ostream& operator<<(...)
视为成员函数(使用成员变量和 this
)。不是这种情况。您可能在 class 中声明它,但它绝对不是 class 的一部分。在使用 this->
的地方使用 mat.
,在只使用 rows
/cols
的地方使用 mat.rows
/mat.cols
.
我也不得不稍微修改一下构造函数,但我仍然不知道为什么它会抱怨。