template link error

Hi,

I have a problem linking to my template operator functions. They are declared and defined in the header so I don't actually know why the compiler can't find it.

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
34
35
36
37
38
39

// Matrix.h
#ifndef MATRIX_H
#define MATRIX_H

namespace math {
   template<_DataType>
   class Matrix;

   template<_T>
   Matrix<_T> operator*(const Matrix<_T> &lhs, const Matrix<_T> &rhs){
      // function body defined here
   }

   template<_T>
   class Matrix {
   public:
      friend Matrix<_T> operator*(const Matrix<_T> &, const Matrix<_T> &);

      Matrix& operator*=(const Matrix& rhs){
         *this = *this * rhs;
         return *this;
      }
   };
};

#endif

// main.cpp
#include "Matrix.h"

using namespace math;

int main{
  Matrix<double> m;

  // initialize some values in m
  m *= m;
}


when i build i get a error LNK2019: unresolved external symbol message saying it can't find the operator*<double> function. not sure what is going since i am defining the template in header and not elsewhere.

I'm using VS2008.

thanks.

Mike

change line 18 to
1
2
template< typename T> friend
Matrix<T> operator*( const Matrix<T>&, const Matrix<T>& );


(Note: do not use _T in the above lines)

Not sure about some of your syntax because GCC does not like

template<_DataType>

it wants

template< class _DataType>

I think your compiler is non-standard.
oops sorry template<_DataType> was a typo.

your fix solved my problem. thanks!

mike
Topic archived. No new replies allowed.