Connecting Tech Pros Worldwide Forums | Help | Site Map

Overloading operator<< as a friend to a template class

CrimzonRJ@gmail.com
Guest
 
Posts: n/a
#1: Sep 30 '06
Theoretically, how are you supposed to overload << as friend to a
template class?

I keep getting an 'unresolved extern symbol' error.

Is this even a smart thing to do?


lw1a2
Guest
 
Posts: n/a
#2: Sep 30 '06

re: Overloading operator<< as a friend to a template class



CrimzonRJ@gmail.com wrote:
Quote:
Theoretically, how are you supposed to overload << as friend to a
template class?
>
I keep getting an 'unresolved extern symbol' error.
>
Is this even a smart thing to do?
example:

#include <iostream>
using namespace std;

template <class T>
class A;

template <class T>
ostream& operator<<(ostream& os, const A<T>& a);

template <class T>
class A
{
friend ostream& operator<< <T>(ostream& os, const A&);
public:
A(T i, T j):i(i), j(j){}
private:
T i;
T j;
};

template <class T>
ostream& operator<<(ostream& os, const A<T>& a)
{
os<<"( "<<a.i<<", "<<a.i<<" )";
return os;
}

int main()
{
A<inta(1, 1);
cout<<a<<endl;
system("pause");
return 0;
}

CrimzonRJ@gmail.com
Guest
 
Posts: n/a
#3: Sep 30 '06

re: Overloading operator<< as a friend to a template class


I am writing the program with Visual Studio 2005 and there is a catch22
involved:
The problem is that the export keyword is not supported therefore I am
forced to put the template Implementations in the header file(is this
correct?).

lw1a2
Guest
 
Posts: n/a
#4: Sep 30 '06

re: Overloading operator<< as a friend to a template class


plaster your code

Greg Comeau
Guest
 
Posts: n/a
#5: Sep 30 '06

re: Overloading operator<< as a friend to a template class


In article <1159604007.669082.323070@m73g2000cwd.googlegroups .com>,
CrimzonRJ@gmail.com <CrimzonRJ@gmail.comwrote:
Quote:
>I am writing the program with Visual Studio 2005 and there is a catch22
>involved:
>The problem is that the export keyword is not supported therefore I am
>forced to put the template Implementations in the header file(is this
>correct?).
Right, but a quick skim shows lw1a2 gave a sample that you would
put in the header file. How is your code different?

You may also wanna have a look at:
http://www.comeaucomputing.com/techt...plates/#export
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Closed Thread