Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with operator << and templates

Reply
Thread Tools

Problem with operator << and templates

 
 
Darío Griffo
Guest
Posts: n/a
 
      08-02-2008
I'm having an error with this code

#include <iostream>

template < typename T> class TestOpTemplate
{
public:
friend std:stream& operator<< <>(std:stream& 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:stream& (:perator<< <> ) (std:stream& os, const
TestOpTemplate<T>& m);

still doesn't work
Any help please
 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      08-02-2008

"Darío Griffo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm having an error with this code
>
> #include <iostream>
>
> template < typename T> class TestOpTemplate
> {
> public:
> friend std:stream& operator<< <>(std:stream& os, const
> TestOpTemplate<T>& m);
> };


#include <iostream>

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

regards
Andy Little


 
Reply With Quote
 
 
 
 
Darío Griffo
Guest
Posts: n/a
 
      08-02-2008
On Aug 2, 5:04*pm, "kwikius" <(E-Mail Removed)> wrote:
> #include <iostream>
>
> template < typename T> class TestOpTemplate
> {
> public:
> friend std:stream& operator<<(std:stream& os, const
> TestOpTemplate<T>& m);
>
> };
>
> regards
> Andy Little


But when I implement the function:

template < typename T> std:stream& operator<<(std:stream& os,
const TestOpTemplate<T>& m)
{
return os
}


i get this

test.cpp:6: warning: friend declaration 'std:stream&
operator<<(std:stream&, 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
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      08-02-2008

"Darío Griffo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Aug 2, 5:04 pm, "kwikius" <(E-Mail Removed)> wrote:
> #include <iostream>
>
> template < typename T> class TestOpTemplate
> {
> public:
> friend std:stream& operator<<(std:stream& os, const
> TestOpTemplate<T>& m);
>
> };
>
> regards
> Andy Little


But when I implement the function:

template < typename T> std:stream& operator<<(std:stream& os,
const TestOpTemplate<T>& m)
{
return os
}


i get this

test.cpp:6: warning: friend declaration 'std:stream&
operator<<(std:stream&, 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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-02-2008
On Aug 2, 10:04 pm, "kwikius" <(E-Mail Removed)> wrote:
> "Darío Griffo" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...


> > I'm having an error with this code


> > #include <iostream>


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


> #include <iostream>


> template < typename T> class TestOpTemplate
> {
> public:
> friend std:stream& operator<<(std:stream& 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:stream& ) ;
friend std:stream& operator<<(
std:stream& 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:stream& operator<<(
std:stream& 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:stream& ) ;
} ;

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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Copy Constructor and overloaded assignment operator with templates saxman C++ 2 06-12-2007 02:49 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Class templates and friend function templates BigMan C++ 1 07-23-2005 09:24 PM
Templates: "implicit typename is deprecated" error and typedef'ing templates Generic Usenet Account C++ 3 07-14-2005 08:02 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments