C++ question (specifically GCC)
I am defining a template class, and am having trouble with defining the <<
operator for that class, for example:
template <class Type>
class Foo {
public:
// ... constructors, destructor et. al
friend ostream &
operator<<(ostream &, const Foo<Type> &);
// here g++ generates the following warning:
warning: friend declaration `class ostream & operator <<(ostream &, const
Foo<Type> &)'
warning: declares a non-template function
// ... rest of Foo
};
// And the implementation
template <class Type>
ostream &operator<<(ostream &os, const Foo<Type> &foo)
{
os << foo;
}
I've tried a few different ways of doing this. Of course I can simply
define my own member function (print), but I prefer the class to overload
the output operator.
--
Jerry Feldman
Alle 15:46, mercoledì 25 dicembre 2002, Jerry Feldman ha scritto:
I am defining a template class, and am having trouble with defining the << operator for that class, for example: template <class Type> class Foo { public: // ... constructors, destructor et. al friend ostream & operator<<(ostream &, const Foo<Type> &); // here g++ generates the following warning: warning: friend declaration `class ostream & operator <<(ostream &, const Foo<Type> &)' warning: declares a non-template function
Declare it like a template as you did below. Of course add the keyword "friend". I tried it with the Suse 8 (gcc 2.95.x) compiler with no problem at all.
// ... rest of Foo };
// And the implementation template <class Type> ostream &operator<<(ostream &os, const Foo<Type> &foo) { os << foo; }
1) This function is missing something to return (usually return os;) 2) This function recursively call itself (and it will never stop) Is this just to make us see how you defined the function?
participants (2)
-
Jerry Feldman
-
Praise