我有一个矩阵:
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
Matrix(int rows, int columns);
Matrix(int, int, int** matrix);
Matrix(Matrix* copy);
~Matrix();
void Set(int, int, int);
void SetMatrix(int, int, int** matrix);
void Print();
void ZeroMatrix(int,int,int** matrix);
void Add(Matrix* B);
void Subtract(Matrix* B);
void Copy(Matrix* B);
int** Multiply(Matrix* B);
int** Create(int,int);
int** Get();
int** Transpose();
int** Scalar(int);
int Get(int,int);
int Rows();
int Columns();
Matrix operator*(int);
private:
int** _matrix;
int _rows;
int _columns;
};
#endif
这是实现:
Matrix Matrix::operator*(int scale)
{
return Matrix(_rows, _columns, Scalar(scale));
}
对于学校作业,我们必须重载多重运算符以使用整数标量。问题是我不断收到此错误:
main.cpp:在函数'int main(int,char * )'中:main.cpp:18:15:错误:'4 * B'中的'operator'不匹配</p>
破解代码:
#include "Matrix.h"
#include <fstream>
#include <iostream>
int main(int argc, char *argv[])
{
Matrix* A = new Matrix(4,2);
A->Set(0,0,1);
A->Set(0,1,2);
A->Set(1,0,3);
A->Set(1,1,4);
A->Print();
Matrix B(A);
B.Print();
Matrix C(4 * B); //this line
C.Print();
delete A;
return 0;
}
有任何想法吗?
编辑#1:
编码:
Matrix operator*(int);
Matrix operator* (int, const Matrix &);
生成:
In file included from main.cpp:1:0:
Matrix.h:31:40: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument
In file included from matrix.cpp:1:0:
Matrix.h:31:40: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument
matrix.cpp:207:50: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument