473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Overloading operator<< as a friend to a template class

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
4 2131

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
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
plaster your code

Sep 30 '06 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
4
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
1
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
0
by: Adrian | last post by:
Hi All, I am trying to create an output operator that doesnt rely on the stream type and is also polymorphic. Is there a way to combine these 2 methods so that the polymorphic output operator...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
10
by: ozizus | last post by:
I overloaded operator << for STL map successfully: template <typename T1, typename T2ostream & operator << (ostream & o, map <T1,T2& m) { //code } the code works like a charm. Now, I want...
2
by: Joe Hesse | last post by:
Hi, I have a template class and I want to define an operator << as a friend function. For each instantiation of the class I want a corresponding instantiation of operator <<. The following...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.