| View previous topic :: View next topic |
| Author |
Message |
desalvo123@comcast.net Guest
|
Posted: Sat Jul 23, 2005 6:39 pm Post subject: 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);
}
---------------------
|
|
| Back to top |
|
 |
Peter Julian Guest
|
Posted: Sun Jul 24, 2005 4:49 am Post subject: Re: overloaded friend operators in template class |
|
|
<desalvo123 (AT) comcast (DOT) net> wrote
| Quote: | 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;
}
|
|
| Back to top |
|
 |
desalvo123@comcast.net Guest
|
Posted: Sun Jul 24, 2005 6:40 pm Post subject: Re: overloaded friend operators in template class |
|
|
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 &)"
|
|
| Back to top |
|
 |
Peter Julian Guest
|
Posted: Mon Jul 25, 2005 9:00 pm Post subject: Re: overloaded friend operators in template class |
|
|
<desalvo123 (AT) comcast (DOT) net> wrote
| Quote: | 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 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
{
os << d.m_value;
return os;
}
}; // class MyDouble
#endif // MYDOUBLE_H_ include guard
__________ Proj_Test.cpp___________
// Proj_Test.cpp.
//
#include
#include "MyDouble.h"
int main()
{
MyDouble<double> md0(1.1);
std::cout << "md0 = " << md0 << "n";
MyDouble
std::cout << "md1 = " << md1 << "n";
MyDouble
std::cout << "result = " << md_result << "n";
return 0;
}
/* output
md0 = 1.1
md1 = 2.2
result = 3.3
*/
|
|
| Back to top |
|
 |
desalvo123@comcast.net Guest
|
Posted: Fri Jul 29, 2005 8:46 pm Post subject: Re: overloaded friend operators in template class |
|
|
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.
|
|
| Back to top |
|
 |
|