Overloading operator<< as a friend to a template class

C

CrimzonRJ

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

lw1a2

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<int> a(1, 1);
cout<<a<<endl;
system("pause");
return 0;
}
 
C

CrimzonRJ

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?).
 
G

Greg Comeau

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/techtalk/templates/#export
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top