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

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 1858

"Darío Griffo" <da*****************@gmail.comwrote in message
news:b6**********************************@a1g2000h sb.googlegroups.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.ukwrote:
#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.comwrote in message
news:3c**********************************@c58g2000 hsc.googlegroups.com...
On Aug 2, 5:04 pm, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
#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.ukwrote:
"Darío Griffo" <dario.griffo.lis...@gmail.comwrote in message
news:b6**********************************@a1g2000h sb.googlegroups.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 IOStreamOperators
{
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 IOStreamOperators< 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 IOStreamOperators,
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 objektorientierter Datenverarbeitung
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
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): ...
2
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...
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>&...
5
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> ...
4
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...
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...
5
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...
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?...
1
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. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.