[英]Overloading operator* for a matrix class
I have a matrix: 我有一个矩阵:
#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
Here's the implementation: 这是实现:
Matrix Matrix::operator*(int scale)
{
return Matrix(_rows, _columns, Scalar(scale));
}
And for a school assignment we have to overload the multiple operator to work with integer scalar. 对于学校作业,我们必须重载multi运算符以使用整数标量。 The problem is I keep getting this error:
问题是我不断收到此错误:
main.cpp: In function 'int main(int, char* )': main.cpp:18:15: error: no match for 'operator ' in '4 * B' main.cpp:在函数'int main(int,char * )'中:main.cpp:18:15:错误: '4 * B'中的'operator ' 不匹配
Breaking code: 破解代码:
#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;
}
Any ideas? 有任何想法吗?
edit # 1: 编辑#1:
the code: 编码:
Matrix operator*(int);
Matrix operator* (int, const Matrix &);
generates: 产生:
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
When you specify your member function, your class must be the left hand side. 当指定成员函数时,您的类必须在左侧。
B * 4
is equivalent to B.operator* (4)
. B * 4
等效于B.operator* (4)
。 When you say 4 * B
, this does not work. 当您说
4 * B
,这是行不通的。
To remedy this, simply use B * 4
instead of 4 * B
, or provide an external overload 为了解决这个问题,只需使用
B * 4
而不是4 * B
,或提供一个外部重载
Matrix operator* (int, const Matrix &);
Then, the 4 * B
will match this overload. 然后,
4 * B
将匹配此过载。
This will work in either direction... 这将在任一方向上起作用...
#include <iostream>
class Matrix
{
public:
Matrix(int x) // This works as a convert constructor
: _x(x) { } // if you don't use the explicit keyword
friend Matrix operator*(const Matrix& left, const Matrix& right);
int _x;
};
Matrix operator*(const Matrix& left, const Matrix& right)
{
return Matrix(left._x * right._x);
}
int main()
{
Matrix m(3);
int a = 4;
Matrix m1(m * a);
Matrix m2(a * m);
std::cout << m._x << endl // 3
<< a << endl // 4
<< m1._x << endl // 12
<< m2._x << endl; // 12
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.