template<T> operator+. linker error

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

Thomas Tutone

tuko said:
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:

Ttry removing the following line, and then see if your linker error
disappears. The following line does not do what you think it does.
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.


Best regards,

Tom
 
M

mlimber

Thomas said:
P.S. - that's a memory leak.

Unless the user does something equally awkward:

tran<double>& t = m1+m2;
delete &t;

In any case, it's a practice to avoid!

Cheers! --M
 
T

Thomas Tutone

mlimber said:
Unless the user does something equally awkward:

tran<double>& t = m1+m2;
delete &t;

Which wouldn't work for temporaries:

tran<double>& t = m1 + m2 + m1;
delete &t;

Now there's a memory leak even using your method.
In any case, it's a practice to avoid!

Agreed.

Best regards,

Tom
 
M

mlimber

Thomas Tutone wrote:
[snip]
Which wouldn't work for temporaries:

tran<double>& t = m1 + m2 + m1;
delete &t;

Now there's a memory leak even using your method.

Good point. Let's all avoid doing what the OP did!

M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top