Reputation: 193
I have a template class matrix
and I try to override the +
operation for different matrix. I want to override the operator in a derived class but have a few errors. In other words, I want to be able to do only A+B
for 2 defined objects of matrix
and not to to do a for
loop.
Here is the code:
#include<iostream>
using namespace std;
template<class T>
class matrix
{
protected:
T **tab;
int row,col;
public:
matrix(int row,int col)
{
this->row = row;
this->col = col;
tab = new T* [col];
for(int i = 0; i < col; i++)
tab[i] = new T[row];
tab[0][0] = 100;
}
T& operator()(int i,int j)
{
return tab[i][j];
}
T operator()(int i,int j)const
{
return tab[i][j];
}
void operator=(const matrix& that)
{
for(int i = 0; i < col ; i++)
for(int j = 0; j < row ; j++)
tab[i][j] = that.tab[i][j];
}
matrix(const T& tab)
{
row = tab.row;
col = tab.col;
tab = new T* [col];
for(int i = 0; i < col; i++)
tab[i] = new T[row];
}
~matrix()
{
for(int i = 0; i < col; i++)
delete tab[i];
delete tab;
}
};
template<class T>
class Operations: public matrix<T>
{
T& operator+(matrix& tab1,matrix& tab2)
{
int i,j;
ligne = tab.ligne;
col = tab.col;
tab3 = new T* [col];
for(int i = 0; i < col; i++)
tab3[i] = new T[ligne];
for(i = 0; i < col; i++)
for(j = 0; j < ligne; j++)
tab3[i][j] = tab1[i][j]+tab2[i][j];
return tab3;
}
};
int main()
{
matrix<int> tab1(10,10);
matrix<int> tab2(5,5);
cout<<tab1(0,0)<<endl;
tab1(0,0) = 10;
cout<<tab2(0,0)<<endl;
tab2(0,0) = 5;
tab2 = tab1;
tab1(0,0) = 3;
return 0;
}
The class matrix
works fine. It is when I added the Operations
class that I got the following erros:
v2.cpp:65:15: error: declaration of ‘operator+’ as non-function
T& operator+(matrix& tab1,matrix& tab2)
^
v2.cpp:65:13: error: expected ‘;’ at end of member declaration
T& operator+(matrix& tab1,matrix& tab2)
^
v2.cpp:65:21: error: expected ‘)’ before ‘&’ token
T& operator+(matrix& tab1,matrix& tab2)
Can you please explain me the reason for those errors and how to correct them? Thank you
EDIT
I'd like to implement the + operator as a member class function in the matrix
, but I am getting an error message: Can you please tell me what is going wrong with the way I define operator+?
matrix operator+(const matrix& that)const
{
int i,j;
T** tab3;
tab3 = new T* [col];
for(int i = 0; i < col; i++)
tab3[i] = new T[row];
for(i = 0; i < col; i++)
for(j = 0; j < row; j++)
tab3[i][j] = that.tab[i][j]+tab[i][j];
return tab3;
}
Upvotes: 1
Views: 121
Reputation: 36637
operator+()
is normally expected to either be a member of the class it acts on (and accept one argument) or be a non-member that accepts two arguments.
For example;
class matrix
{
public:
matrix operator+(const matrix &) const; // member version
};
or
class matrix
{
public:
friend matrix operator+(const matrix &, const matrix &);
};
matrix operator+(const matrix &, const matrix &);
Note that friendship is optional in the second case, if the matrix provides public member functions that the operator+()
needs.
The two approaches cannot be mixed. If the compiler encounters a class with both forms of operator+()
, it has no basis to prefer calling one over the other, and will reject code like c = a+b
due to ambiguity.
It is not also possible for another class (like your Operations
) to provide a non-static member function operator+()
that accepts two arguments of type matrix
. The error message you are getting about a "non-function" is referring to that (albeit the compiler vendor has used rather cryptic wording for the error message).
Also note that operator+()
should generally return by value, rather than returning a reference. Returning a reference usually causes expression like c = a+b
to work differently than expected - and (depending on what reference is returned) can cause the caller to exhibit undefined behaviour.
Upvotes: 4
Reputation:
I think you're just supposed to overload the + operator for the matrix
class
matrix& operator+(matrix& other){
....
}
You can therefor use it like :
matrix<type> a;
// initialise a
matrix<type> b;
// initialise b
matrix<type> c = a+b; // a+b <=> a.operator+(b);
Upvotes: 0