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

1
2
3
4
5
6
7
    #define TEMPLATEMATRIX template<class T, int C, int R>
    TEMPLATEMATRIX Matrix<T, C, R> operator* (Matrix<T, C, R> a, Matrix<T, C, R> b);
    TEMPLATEMATRIX class Matrix{
    ....
    friend Matrix<T, C, R> operator* <>(Matrix<T, C, R> a, Matrix<T, C, R> b);
    ...
    };

And it works like a charm,

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define TEMPLATEMATRIXT template<class T, int C, int R, int R1>
    TEMPLATEMATRIXT Matrix<T, R1, R> operator* (Matrix<T, R1, C> a, Matrix<T, C, R> b);
    
    TEMPLATEMATRIX class Matrix{
    ....
    template<int R1> friend Matrix<T, R1, R> operator* (Matrix<T, R1, C> a, Matrix<T, C, R> b);
    ...
    };
    
    //Multiplication
    TEMPLATEMATRIXT Matrix<T, R1, R> operator* (Matrix<T, R1, C> a, Matrix<T, C, R> b){
      Matrix<T, R1, R> t;
      ...
      return t;
    }


It compiles, but when i do.

1
2
3
4
5
       Undefined symbols for architecture x86_64:
      "Matrix<int, 100, 19> operator*<100>(Matrix<int, 100, 123>, Matrix<int, 123, 19>)", 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()

1
2
3
4
      Matrix<int, 123, 19> ta2;
      
      Matrix<int, 100, 123> ta;
      Matrix<int, 100, 19> 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define TEMPLATEMATRIX template<class T, int C, int R>
#define TEMPLATEMATRIXT template<class T, int C, int R, int R1>
#include <iostream>
#include <sstream>

using namespace std;

TEMPLATEMATRIX class Matrix;

//Multiplication
TEMPLATEMATRIXT Matrix<T, R1, R> operator* (Matrix<T, R1, C> a, Matrix<T, C, R> b);
TEMPLATEMATRIX Matrix<T, C, R> operator* (Matrix<T, C, R> a, Matrix<T, C, R> b);
...

TEMPLATEMATRIX class Matrix{
  private:
    T matrixData[C][R];
    int rows, columns;
  public:
    Matrix(){
      this->rows=R;
      this->columns=C;
      for(int x=0;x<C;x++)
        for(int y=0;y<R;y++)
          this->matrixData[x][y]=0;
    }
    T& operator()(int, int);
    
    //Multiplication
    template<class T, int C, int R, int R1> friend Matrix<T, R1, R> operator* (Matrix<T, R1, C> a, Matrix<T, C, R> b);
    friend Matrix<T, C, R> operator* <>(Matrix<T, C, R> a, Matrix<T, C, R> b);
    friend Matrix<T, C, R> operator* <>(Matrix<T, C, R> 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.