Template for specific method

I'm creating a class for matrix.
It's a homework with templates, so i created 2 methods for multiplication, one where the matrix are equal the other is when i have just [x][y] [y][z].
Then i created the first one

 ``1234567`` `````` #define TEMPLATEMATRIX template TEMPLATEMATRIX Matrix operator* (Matrix a, Matrix b); TEMPLATEMATRIX class Matrix{ .... friend Matrix operator* <>(Matrix a, Matrix b); ... };``````

And it works like a charm,

But when i tried to implement the second one i had some problems, i solved it with:

 ``123456789101112131415`` ``````#define TEMPLATEMATRIXT template TEMPLATEMATRIXT Matrix operator* (Matrix a, Matrix b); TEMPLATEMATRIX class Matrix{ .... template friend Matrix operator* (Matrix a, Matrix b); ... }; //Multiplication TEMPLATEMATRIXT Matrix operator* (Matrix a, Matrix b){ Matrix t; ... return t; }``````

It compiles, but when i do.

 ``12345`` `````` Undefined symbols for architecture x86_64: "Matrix operator*<100>(Matrix, Matrix)", referenced from: _main in cciAzuMs.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status``````

like the method was not implemented.

How i do in the main()

 ``1234`` `````` Matrix ta2; Matrix ta; Matrix rr=ta*ta2;``````

i don't know if i implemented this template right.

Thanks

That isn't quite right. Templates are used to abstract type from an algorithm/class. You're just using it to pass in parameters, which you can do (and should do) with regular parameters.

Consider these.

Does the multiplication procedure change if you have an Matrix of floats, doubles, ints or complex numbers? No.

Does the multiplication procedure change if your matrix has a different number elements per dimension? Yes, only certain patterns are allowed.

In general, your templates should parametrise type, not values.
The problem is, i need to fix the param C, beacuse the matrix need to have the inner sizes equal.
And in this case its not only for floats, doubles and ints, its a homework and the ideia is that i can implement a operator* in my code.

I just can't figure out where is the problem, why it dont compile (find the symbol)?
Having the dimensions as template arguments is fine.

The problem is your friend declaration. The `template<int R1>` makes operator* become a template function with one template argument. You need to have all 4 template arguments because that is what you use when you define it.
`template<class T2, int C2, int R2, int R12> friend Matrix<T2, R12, R2> operator* (Matrix<T2, R12, R2> a, Matrix<T2, C2, R2> b);`
Hi,

When i do that i receive.

matrix.h:59: error: declaration of ‘class T’
tmatrix.h:44: error: shadows template parm ‘class T’
tmatrix.h:59: error: declaration of ‘int C’
tmatrix.h:44: error: shadows template parm ‘int C’
tmatrix.h:59: error: declaration of ‘int R’
tmatrix.h:44: error: shadows template parm ‘int R’

My class:

 ``123456789101112131415161718192021222324252627282930313233`` ``````#define TEMPLATEMATRIX template #define TEMPLATEMATRIXT template #include #include using namespace std; TEMPLATEMATRIX class Matrix; //Multiplication TEMPLATEMATRIXT Matrix operator* (Matrix a, Matrix b); TEMPLATEMATRIX Matrix operator* (Matrix a, Matrix b); ... TEMPLATEMATRIX class Matrix{ private: T matrixData[C][R]; int rows, columns; public: Matrix(){ this->rows=R; this->columns=C; for(int x=0;xmatrixData[x][y]=0; } T& operator()(int, int); //Multiplication template friend Matrix operator* (Matrix a, Matrix b); friend Matrix operator* <>(Matrix a, Matrix b); friend Matrix operator* <>(Matrix a, T b); ....``````
Last edited on
ppl, you saved my life.

Thanks so muchh.

The problem was the 2 templates with the same variables name
Topic archived. No new replies allowed.