473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1396
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
2213
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 amount my operator has for bother cin , cout operator<< and >> thanks Dan
3
2940
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 FUNCTION!!
8
2004
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>& lst );
2
2161
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 Debug_Level ddl;
4
2492
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
1313
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 has template arguments. I have tried a few ways and my compiler just wont let me. Method 1: Doesn't rely on the char type of a stream...
4
1770
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? The code is as follows... // main class: template < class Individual,
2
1922
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 example fails to compile with g++ version 4.1.2. I would appreciate it if you could help me fix it or point me to a suitable reference. Thank you,
4
1879
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
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.