Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloading operator<< as a friend to a template class

Reply
Thread Tools

Overloading operator<< as a friend to a template class

 
 
CrimzonRJ@gmail.com
Guest
Posts: n/a
 
      09-30-2006
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?

 
Reply With Quote
 
 
 
 
lw1a2
Guest
Posts: n/a
 
      09-30-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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<int> a(1, 1);
cout<<a<<endl;
system("pause");
return 0;
}

 
Reply With Quote
 
 
 
 
CrimzonRJ@gmail.com
Guest
Posts: n/a
 
      09-30-2006
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?).

 
Reply With Quote
 
lw1a2
Guest
Posts: n/a
 
      09-30-2006
plaster your code

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-30-2006
In article <(E-Mail Removed) .com>,
(E-Mail Removed) <(E-Mail Removed)> wrote:
>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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
Problem with defining template friend function of a template class. PengYu.UT@gmail.com C++ 2 11-09-2005 08:27 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
using friend function template in class template Yueh-Wei Hu C++ 0 05-23-2004 11:36 AM



Advertisments