T
tuko
The following snipet gives a linker error.
I don't get it...
template<class T> class tran {
public:
public:
private:
};
template<class T> class matrix {
public:
matrix(int n);
public:
friend tran<T>& operator+(const matrix &m1, const matrix &m2);
private:
int n_;
};
template<class T>
matrix<T>::matrix(int n) : n_(n) {}
template<class T>
tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
tran<T> *to = new tran<T>;
return *to;
}
int main () {
matrix<double> mat1(10), mat2(10), mat3(10);
//
operator+<double>(mat1, mat2); // point 1
mat1+mat2; // point 2
//
return 0;
}
In the code above...
Point 1. Calling explicitly the operator+.
It compiles fine.
Point 2. Gives linking error.
F:\DOCUME~1\alan\LOCALS~1\Temp/cc2Faaaa.o(.text+0x70):matrix.cpp:
undefined reference to `operator+(matrix<double> const&, matrix<double> const&)'
I'm doing something wrong. I don't know what.
Any help would be greatly appreciated.
I don't get it...
template<class T> class tran {
public:
public:
private:
};
template<class T> class matrix {
public:
matrix(int n);
public:
friend tran<T>& operator+(const matrix &m1, const matrix &m2);
private:
int n_;
};
template<class T>
matrix<T>::matrix(int n) : n_(n) {}
template<class T>
tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
tran<T> *to = new tran<T>;
return *to;
}
int main () {
matrix<double> mat1(10), mat2(10), mat3(10);
//
operator+<double>(mat1, mat2); // point 1
mat1+mat2; // point 2
//
return 0;
}
In the code above...
Point 1. Calling explicitly the operator+.
It compiles fine.
Point 2. Gives linking error.
F:\DOCUME~1\alan\LOCALS~1\Temp/cc2Faaaa.o(.text+0x70):matrix.cpp:
undefined reference to `operator+(matrix<double> const&, matrix<double> const&)'
I'm doing something wrong. I don't know what.
Any help would be greatly appreciated.