By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Overloading operator<< as a friend to a template class

P: n/a
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?

Sep 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Cr*******@gmail.com wrote:
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;
}

Sep 30 '06 #2

P: n/a
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?).

Sep 30 '06 #3

P: n/a
plaster your code

Sep 30 '06 #4

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .com>,
Cr*******@gmail.com <Cr*******@gmail.comwrote:
>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?
Sep 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.