473,327 Members | 1,979 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,327 software developers and data experts.

overloaded friend operators in template class

This compile and works fine.

Example 1:
---------------------
class MyDouble
{
public:
MyDouble(double val) { m_Value = val; };
friend MyDouble operator + (const MyDouble&, const MyDouble&);

private:
double m_Value;
};

MyDouble operator + (const MyDouble &left, const MyDouble &right)
{
return MyDouble(left.m_Value + right.m_Value);
}
---------------------

But I can't figure out how to do the same thing in a template class.
I've tried Example 2 but it won't compile. I am using Visual Studio
..NET 2003.
If I take out the <> in the declartion of the operator + it compiles
but then if I try to use that operator I get an unresolved external
symbol error.

Example 2:
---------------------
template <class T> class MyDouble
{
public:
MyDouble<T>(T val) { m_Value = val; };
friend MyDouble<T> operator + <>(const MyDouble&, const MyDouble&);

private:
T m_Value;
};

template <class T> MyDouble<T> operator + (const MyDouble<T> &left,
const MyDouble<T> &right)
{
return MyDouble<T>(left.m_Value + right.m_Value);
}
---------------------

Jul 23 '05 #1
4 1884

<de********@comcast.net> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
This compile and works fine.

Example 1:
---------------------
class MyDouble
{
public:
MyDouble(double val) { m_Value = val; };
friend MyDouble operator + (const MyDouble&, const MyDouble&);

private:
double m_Value;
};

MyDouble operator + (const MyDouble &left, const MyDouble &right)
{
return MyDouble(left.m_Value + right.m_Value);
}
---------------------

But I can't figure out how to do the same thing in a template class.
I've tried Example 2 but it won't compile. I am using Visual Studio
.NET 2003.
If I take out the <> in the declartion of the operator + it compiles
but then if I try to use that operator I get an unresolved external
symbol error.

Example 2:
---------------------
template <class T> class MyDouble
{
public:
MyDouble<T>(T val) { m_Value = val; };
friend MyDouble<T> operator + <>(const MyDouble&, const MyDouble&);

private:
T m_Value;
};

template <class T> MyDouble<T> operator + (const MyDouble<T> &left,
const MyDouble<T> &right)
{
return MyDouble<T>(left.m_Value + right.m_Value);
}
---------------------


Binary operator+ returns a const value. ctor should rely on the
initialization list.

template <class T>
class MyDouble
{
T m_value;
public:
MyDouble(T val) : m_value(val) { };
~MyDouble() { }

friend const MyDouble<T> /* operator+ */
operator+(const MyDouble<T>&, const MyDouble<T>&);
};

template <class T>
const MyDouble<T> /* operator+ */
operator+(const MyDouble<T>& left, const MyDouble<T>& right)
{
return MyDouble<T>(left.m_value + right.m_value);
}

int main()
{
MyDouble<double> md0(1.1);
MyDouble<double> md1(2.2);

MyDouble<double> md_result = md0 + md1;

return 0;
}

Jul 24 '05 #2
I tried you code but I still get an unresolved symbol error.

error LNK2001: unresolved external symbol "class MyDouble<double> const
__cdecl operator+(class MyDouble<double> const &,class MyDouble<double>
const &)"

Jul 24 '05 #3

<de********@comcast.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I tried you code but I still get an unresolved symbol error.

error LNK2001: unresolved external symbol "class MyDouble<double> const
__cdecl operator+(class MyDouble<double> const &,class MyDouble<double>
const &)"


Its telling you it can't find the supplied prototype's implementation. Have
you tried cleaning and rebuilding the project? Are you running the project
in debug without the dedug dlls (a known VS.net issue)? Are you seperating
declaration and implementation of MyDouble? If yes, can you post what you
ran exactly?

I ran the aforementioned code with VS, g++ and checked with comeau's online
compiler (which found one error which should be inconsequential):

MyDouble(T val) : m_value(val) { }; << extra semicolon

the ctor should have been:

MyDouble(T val) : m_value(val) { }

Here is the code using a seperate header for MyDouble class and the friend
functions are implemented without protyping (in case your compiler has a bug
with such template-friend prototypes):

________MyDouble.h____________

/* MyDouble.h ... MyDouble class */
#ifndef MYDOUBLE_H_
#define MYDOUBLE_H_

template <class T>
class MyDouble
{
T m_value;
public:
MyDouble(T val) : m_value(val) { }
~MyDouble() { }

// friend operator+
friend const MyDouble<T> /* rearranged to fit screen width */
operator+(const MyDouble<T>& left, const MyDouble<T>& right)
{
return MyDouble<T>(left.m_value + right.m_value);
}

// friend operator<<
friend std::ostream& /* rearranged to fit screen width */
operator<<(std::ostream& os, const MyDouble<T>& d)
{
os << d.m_value;
return os;
}
}; // class MyDouble

#endif // MYDOUBLE_H_ include guard

__________ Proj_Test.cpp___________

// Proj_Test.cpp.
//
#include <iostream>
#include "MyDouble.h"

int main()
{
MyDouble<double> md0(1.1);
std::cout << "md0 = " << md0 << "\n";
MyDouble<double> md1(2.2);
std::cout << "md1 = " << md1 << "\n";

MyDouble<double> md_result = md0 + md1;

std::cout << "result = " << md_result << "\n";

return 0;
}

/* output

md0 = 1.1
md1 = 2.2
result = 3.3

*/

Jul 25 '05 #4
Yes, that works, but it is different then my example. My problem
occurs if the functions are defined outside the class, like I showed in
my first post.

Jul 29 '05 #5

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
1
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's...
7
by: Riku Jarvinen | last post by:
Hello everyone, I have a logging class which writes program outputs to the logfile. The class works fine as long as only C++ native data types are considered. The problem is that I have a...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
1
by: Ralph Moritz | last post by:
Hi, I've written a class called ConfigParser, which can be used to parse config files. (Surprise!) I have overloaded the shift operators to mimic the std iostreams. My question is, should...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.