I'm trying to declare a template method on a template class and it's not working for me. It's better to explain by giving the code so here it is: I have this class:
matrix.h
template <class T,int a,int b>
class Matrix {
private:
int x;
int y;
public:
class IllegalOperation();
template<T,int c,int d>
Matrix<T,a,b> operator+(const Matrix<T,c,d> m);
//...
}
matrix.cpp
template<class T,int a,int b>
template<T,int c,int d>
Matrix<T,a,b> Matrix<T,a,b>::operator+(const Matrix<T,c,d> m){
if(a!=c || b!=d) throw IllegalOperation();
// add matrices and return the result
}
I'd like this code to work for any 2 types of Matrix and Matrix where a,b,c and d can be different. for example, I want this code to compile and return an error (in run time):
const Matrix<int, 3, 2> m1;
const Matrix<int, 7, 3> m2;
// init m1 and m2
m1+m2;
While this code should compile and run successfully:
const Matrix<int, 3, 2> m1;
const Matrix<int, 3, 2> m2;
// init m1 and m2
m1+m2;
However, when I try to compile the code above I get this error:
no match for âoperator+ in m1+m2