| demonofnight (12) | |||||||||
|
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
And it works like a charm, But when i tried to implement the second one i had some problems, i solved it with:
It compiles, but when i do.
like the method was not implemented. How i do in the main()
i don't know if i implemented this template right. Thanks | |||||||||
|
|
|||||||||
| kbw (5522) | |
|
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. | |
|
|
|
| demonofnight (12) | |
|
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)? | |
|
|
|
| Peter87 (3917) | |
|
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);
| |
|
|
|
| demonofnight (12) | |||
|
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:
| |||
|
Last edited on
|
|||
| demonofnight (12) | |
|
ppl, you saved my life. Thanks so muchh. The problem was the 2 templates with the same variables name | |
|
|
|