Search code examples
c++templatesfriend

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

#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:

#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.

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()

  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


Solution

  • Solved.

    The problem was the

    #define TEMPLATEMATRIX template<class T, int C, int R>
    #define TEMPLATEMATRIXT template<class T, int C, int R, int R1>
    

    I cannot use 2 templates (not in my case) with the same "vars"

    #define TEMPLATEMATRIX template<class T, int C, int R>
    #define TEMPLATEMATRIXT template<class T2, int C2, int R2, int R1>
    

    It solved my problem