[英]Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion)
I have some problem about operator overloading. 我有一些关于运算符重载的问题。 I looked everywhere but couldn't find a proper solution for this error.
我到处寻找,但无法找到适合此错误的解决方案。 Here is some parts of my code :
以下是我的代码的一些部分:
Matrix<type> Matrix<type>::operator/(const Matrix& denom){
if(num_of_rows != denom.num_of_rows || num_of_cols != denom.num_of_cols)
throw string("Unable to divide (Different size).");
if(denom.contains(0))
throw string("Unable to divide (Divide by zero).");
for(int i = 0; i < num_of_rows; i++)
for(int j = 0; j < num_of_cols; j++)
values[i][j] /= denom.values[i][j];
// I KNOW THIS IS NOT HOW TO DIVIDE TWO MATRICES
return *this;
}
void Matrix<type>::operator=(const Matrix& m) const {
delete [][] values;
num_of_rows = m.num_of_rows;
num_of_cols = m.num_of_cols;
values = new type*[num_of_rows];
for(int i = 0; i < num_of_rows; i++){
*(values + i) = new type[num_of_cols];
for(int j = 0; j < num_of_cols; j++)
values[i][j] = m.values[i][j];
}
}
And this is the Matrix class and the constructor takes 2 arguments : 这是Matrix类,构造函数有2个参数:
class Matrix{
private:
type** values;
int num_of_rows, num_of_cols;
public:
Matrix(){}
Matrix(int, int);
type getElement(int, int);
void print();
bool contains(type);
Matrix<type> operator/(const Matrix&);
void operator=(const Matrix&) const;
};
template <class type>
Matrix<type>::Matrix(int rows, int cols){
values = new type*[rows];
num_of_rows = rows;
num_of_cols = cols;
for(int i = 0; i < rows; i++){
*(values + i) = new type[cols];
for(int j = 0; j < cols; j++){
type random = (type)rand() / 3276.71;
values[i][j] = random;
}
}
}
And this piece of code in main gives this error : 而main中的这段代码给出了这个错误:
srand(time(NULL));
Matrix<int> m1(3,5); // creating some objects
Matrix<double> m2(3,5); // matrices’ elements are assigned randomly from 0 to 10
Matrix<double> m3(5,5);
Matrix<double> m4(5,6);
if(!(m2.contains(0))){
Matrix<double> m8(3,5);
m8=m1/m2; // THIS LINE GIVES ERROR
m8.print();
}
m1
has type Matrix<int>
, so when looking up for a suitable overload of operator/
we find: m1
类型为Matrix<int>
,因此在查找适当的operator/
重载时,我们会发现:
Matrix<int> Matrix<int>::operator/(const Matrix& denom);
Note that the parameter type Matrix
here makes use of the so-called injected class name. 请注意,此处的参数类型
Matrix
使用所谓的注入类名。 That means Matrix
stands in this case for Matrix<int>
, since that's the (template) class in question. 这意味着
Matrix
在这种情况下代表Matrix<int>
,因为那是所讨论的(模板)类。 However m2
, the argument to the call to operator/
, has type Matrix<double>
. 但是
m2
,调用operator/
的参数的类型为Matrix<double>
。 There is no suitable conversion from Matrix<double>
to Matrix<int>
so the call is invalid. 从
Matrix<double>
到Matrix<int>
没有合适的转换,因此调用无效。
A possible fix is to change operator/
to also be a template: 可能的解决方法是更改
operator/
也是一个模板:
// Declared inside Matrix<type>:
template<typename Other>
Matrix& operator/=(Matrix<Other> const& other);
(I also took the liberty of fixing the operator to better reflect what it's actually doing.) (我也冒昧地修复操作员以更好地反映它实际上在做什么。)
However you'll then run into the problem that you're using a Matrix<int>
(the result of the call to operator/
) to assign to m8
which has type Matrix<double>
. 但是,您将遇到问题,即您正在使用
Matrix<int>
(调用operator/
的结果)来分配给类型为Matrix<double>
m8
。 So perhaps you need an operator=
to do a conversion (in which case I'd recommend a converting constructor as well, or perhaps even just a converting constructor without a converting operator=
). 所以也许你需要一个
operator=
进行转换(在这种情况下我也会推荐一个转换构造函数,或者甚至只是一个没有转换operator=
的转换构造函数operator=
)。
The error message quite clearly states that you haven't defined a division operator taking the two types as arguments you are passing. 错误消息非常清楚地表明您没有定义一个除法运算符,将两种类型作为您传递的参数。 Looking at the code excerpt this is the case: There is an operator taking two
Matrix<T>
but none taking a Matrix<T1>
and a Matrix<T2>
(for different type T1
and T2
). 看一下代码摘录就是这样的情况:有一个运算符采用两个
Matrix<T>
但没有采用Matrix<T1>
和Matrix<T2>
(对于不同类型的T1
和T2
)。
BTW, what is your question? 顺便问一下,你的问题是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.