Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template<T> operator+. linker error

Reply
Thread Tools

template<T> operator+. linker error

 
 
tuko
Guest
Posts: n/a
 
      09-16-2005
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.

--
tuko.
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2005
tuko wrote:
> 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

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2005
> > template<class T>
> > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
> > tran<T> *to = new tran<T>;
> > return *to;


P.S. - that's a memory leak.


Best regards,

Tom

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-16-2005

Thomas Tutone wrote:
> > > template<class T>
> > > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
> > > tran<T> *to = new tran<T>;
> > > return *to;

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

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-16-2005

mlimber wrote:
> Thomas Tutone wrote:
> > > > template<class T>
> > > > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
> > > > tran<T> *to = new tran<T>;
> > > > return *to;

> >
> > P.S. - that's a memory leak.

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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-16-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linker error - CVTRES : fatal error CVT1100: quad C++ 3 06-08-2007 11:02 PM
c++ linker error, please help! TR C++ 3 06-10-2004 05:13 AM
Q: linker error with a missing method Arne Schmitz C++ 3 04-16-2004 10:05 PM
Linker error with static const field Martin Magnusson C++ 2 10-20-2003 09:36 PM
Linker error - unsolved external symbol Wolfgang Rueckert C++ 0 06-30-2003 10:59 AM



Advertisments