473,614 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with operator << and templates

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);
};

The error is
declaration of 'operator<<' as non-function

Searchin on the web, i've found that it could be solved using
parenthesis aroung the funcion name, but it doesn't

friend std::ostream& (::operator<< <) (std::ostream& os, const
TestOpTemplate< T>& m);

still doesn't work
Any help please
Aug 2 '08 #1
4 1885

"Darío Griffo" <da************ *****@gmail.com wrote in message
news:b6******** *************** ***********@a1g 2000hsb.googleg roups.com...
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);
};
#include <iostream>

template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<<(std: :ostream& os, const
TestOpTemplate< T>& m);
};

regards
Andy Little
Aug 2 '08 #2
On Aug 2, 5:04*pm, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
#include <iostream>

template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<<(std: :ostream& os, const
TestOpTemplate< T>& m);

};

regards
Andy Little
But when I implement the function:

template < typename Tstd::ostream& operator<<(std: :ostream& os,
const TestOpTemplate< T>& m)
{
return os
}
i get this

test.cpp:6: warning: friend declaration 'std::ostream&
operator<<(std: :ostream&, const TestOpTemplate< T>&)' declares a non-
template function
test.cpp:6: note: (if this is not what you intended, make sure the
function template has already been declared and add <after the
function name here)
linking test (libtool)

That's why i've putted <after the name. Compiling with g++ 4.3.1
Aug 2 '08 #3

"Darío Griffo" <da************ *****@gmail.com wrote in message
news:3c******** *************** ***********@c58 g2000hsc.google groups.com...
On Aug 2, 5:04 pm, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
#include <iostream>

template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<<(std: :ostream& os, const
TestOpTemplate< T>& m);

};

regards
Andy Little
But when I implement the function:

template < typename Tstd::ostream& operator<<(std: :ostream& os,
const TestOpTemplate< T>& m)
{
return os
}
i get this

test.cpp:6: warning: friend declaration 'std::ostream&
operator<<(std: :ostream&, const TestOpTemplate< T>&)' declares a non-
template function
test.cpp:6: note: (if this is not what you intended, make sure the
function template has already been declared and add <after the
function name here)
linking test (libtool)

That's why i've putted <after the name. Compiling with g++ 4.3.1

hmmm... Dunno I guess you will have to wait for the experts ... Meanwhile
you could try just defining the function in the class maybe...

regards
Andy Little
Aug 2 '08 #4
On Aug 2, 10:04 pm, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
"Darío Griffo" <dario.griffo.l is...@gmail.com wrote in message
news:b6******** *************** ***********@a1g 2000hsb.googleg roups.com...
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);
};
#include <iostream>
template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<<(std: :ostream& os, const
TestOpTemplate< T>& m);
};
Be careful. That declares a non-template function as friend, so
he'll have to implement a non-template function for every
instance of TestOpTemplate.

I know that the current draft very explicitly provides for three
alternatives with regards to friend: a non-template, a template
for which only the corresponding specialization is a friend, and
a template for which all specializations are friend. For some
reason, however, I think that this is a recent clarification or
fix, and the compilers vary in what they actually implement
(except that all support a non-template as friend in more or
less the same manner). Anyway, my "standard" solution is to
define a public member function print(), and then define the
operator<< inline, which just calls it, e.g.:

template< typename T >
class TestOpTemplate
{
public:
void print( std::ostream& ) ;
friend std::ostream& operator<<(
std::ostream& dest,
TestOpTemplate< T const& obj )
{
obj.print( dest ) ;
return dest ;
}
} ;

Since the friend function is defined each time the template is
specialized, it doesn't matter that it's not a template; you get
a new non-template function for each type.

Of course, in practice, the case comes up fairly often, so I've
moved these functions down into a templated base class, so it's
sufficient that my class derives from it, e.g.:

template< typename T >
struct IOStreamOperato rs
{
friend std::ostream& operator<<(
std::ostream& dest,
T const& obj )
{
obj.print( dest ) ;
return dest ;
}
friend std::istream& operator>>(
std::istream& source,
T& obj )
{
obj.scan( source ) ;
return source ;
}
} ;

template< typename T >
class TestOpTemplate
: public IOStreamOperato rs< TestOpTemplate < T
{
public:
void print( std::ostream& ) ;
} ;

And in case it isn't obvious:

-- the only reason for the friend in these cases is to allow
you to define the non-member function in the class
definition, and

-- if you never used one of the functions in IOStreamOperato rs,
it won't be instantiated, so you won't get an error if the
member function it calls isn't present.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #5

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

Similar topics

4
7147
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
2
5674
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
8
2011
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 );
5
5676
by: Karl | last post by:
Hey everyone! So I'm writing my own String class to wrap std::string and implement an API as close to identical as possible to the Java API. It's pretty small so far: #include <string> class String {
4
2643
by: homsan toft | last post by:
I've tried the below code with MSVC and Comeau online compiler. Both complain that operator<< for Outer<part<size_t> >::inner is not defined. So how do I declare it without doing full specialization? I've tried a template on any type, like Outer<AnyT>::inner (see below) Also tried a template on the templated type: Outer<part<X> >::inner // (see below) I can't find a typo? What is the rule in operation here? Thanks, homsan
0
1316
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 --------------------------------------------------- class A
5
3648
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
4
1773
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,
1
1401
by: Stuart Golodetz | last post by:
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
0
8198
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8591
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8444
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1758
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.