Reputation: 241
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 :
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 :
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();
}
Upvotes: 1
Views: 787
Reputation: 153792
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 aMatrix<T1>
and a Matrix<T2>
(for different type T1
and T2
).
BTW, what is your question?
Upvotes: 0
Reputation: 35439
m1
has type Matrix<int>
, so when looking up for a suitable overload of operator/
we find:
Matrix<int> Matrix<int>::operator/(const Matrix& denom);
Note that the parameter type Matrix
here makes use of the so-called injected class name. That means Matrix
stands in this case for Matrix<int>
, since that's the (template) class in question. However m2
, the argument to the call to operator/
, has type Matrix<double>
. There is no suitable conversion from Matrix<double>
to Matrix<int>
so the call is invalid.
A possible fix is to change operator/
to also be a template:
// 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>
. 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=
).
Upvotes: 3