473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

template<T> operator+. linker error

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.
Sep 16 '05 #1
5 1761
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

Sep 16 '05 #2
> > 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

Sep 16 '05 #3

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

Sep 16 '05 #4

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

Sep 16 '05 #5
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

Sep 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Steven T. Hatton | last post by:
I believe the answer is that the language doesn't support the ability to retain a reference to a std::indirect_array<>, and would be illadvised to try, but I figure I'll ask just to be sure. What...
2
by: Gundolf | last post by:
Hi, I'm wondering if there is some neat way of passing a comparative operator (<, >, = , <=, ...) to a template-style-function. For starters, I guess we've all seen this neat little example: ...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
3
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
3
by: George2 | last post by:
Hello everyone, I sometimes saw code with template<and followed by a class definition, like, template<class { // class definition
9
by: jabbah | last post by:
yeah i know that the title is not really a question - sorry - i fail to put my question into one sentence so i put it into a storry: background is i am using two different 'ARRAY's in my code....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.