473,396 Members | 1,892 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,396 software developers and data experts.

Problem with making an instance of a templated operator<< a friendof a template class

Hi guys,

I'm trying to making an instance of a templated operator<< for a
templated class a friend of that class (see below), to allow it to
access the class internals for output purposes.

#include <iostream>

template <typename T>
class TC
{
friend std::ostream& operator<<(std::ostream& os, const TC&);
};

template <typename Tstd::ostream& operator<<(std::ostream& os, const
TC<T>&)
{
return os;
}

int main()
{
TC<inttc;
std::cout << tc;
return 0;
}

Unfortunately the way I've done it is wrong: I'm referring in the above
to a non-template instance instead of a template one (the online Comeau
compiler kindly warned me about this and recommended I add <>...). This
would be ok normally, but because it's an operator I'm having trouble.
VC++ 2005 refuses (probably correctly?) to accept the following:

friend std::ostream& operator<< <>(std::ostream& os, const TC&);

Please can you tell me if I'm missing something? Is this possible for
operators (have I just messed up the syntax)?

Thanks!
Stu
Jun 27 '08 #1
1 1381
Stuart Golodetz wrote:
Hi guys,

I'm trying to making an instance of a templated operator<< for a
templated class a friend of that class (see below), to allow it to
access the class internals for output purposes.

#include <iostream>

template <typename T>
class TC
{
friend std::ostream& operator<<(std::ostream& os, const TC&);
};

template <typename Tstd::ostream& operator<<(std::ostream& os, const
TC<T>&)
{
return os;
}

int main()
{
TC<inttc;
std::cout << tc;
return 0;
}

Unfortunately the way I've done it is wrong: I'm referring in the above
to a non-template instance instead of a template one (the online Comeau
compiler kindly warned me about this and recommended I add <>...). This
would be ok normally, but because it's an operator I'm having trouble.
VC++ 2005 refuses (probably correctly?) to accept the following:

friend std::ostream& operator<< <>(std::ostream& os, const TC&);

Please can you tell me if I'm missing something? Is this possible for
operators (have I just messed up the syntax)?

Thanks!
Stu
Apologies, I've managed to solve the problem. Here's how, for the record:

#include <iostream>

template <typename Tclass TC;
template <typename Tstd::ostream& operator<<(std::ostream& os, const
TC<T>&);

template <typename T>
class TC
{
friend std::ostream& operator<< <>(std::ostream& os, const TC&);
};

template <typename Tstd::ostream& operator<<(std::ostream& os, const
TC<T>&)
{
return os;
}

int main()
{
TC<inttc;
std::cout << tc;
return 0;
}

Cheers,
Stu
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
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>&...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
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?...
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...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
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: 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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.