Re: compiler error
On May 14, 2:52 pm, Zeppe
<zeppe@.remove.all.this.long.comment.email.it> wrote:
Aston Martin wrote:
> Hello Zeppe, thanks for your advise, I see your point, but also
> disappointed that even this does give exactly the same error
> #include<iostream>
> using namespace std;
> template<typename T, int dummy>
> class SomeClass;
> template<typename T, int dummy>
> ostream& operator << ( ostream& outStream, const SomeClass<T, dummy>&
> sc )
> {
> outStream << "someVariable: " << sc.someVariable << endl;
> return outStream;
> }
> template<typename T, int dummy>
> class SomeClass
> {
> private:
> T someVariable;
> public:
> SomeClass() : someVariable(65) {}
> ~SomeClass() {}
> template<typename T, int dummy>
> friend ostream& operator << ( ostream& outStream, const
> SomeClass<T, dummy>& sc );
you are declaring again your function as a template, while it is not. I
mean, the function is a template on the parameters of SomeClass, but you
don't want to declare the general template function as a friend of
SomeClass<T,dummy>. You want to declare only the function operator<<
<T,dummy>(...) as a friend of SomeClass<T,dummy>. That's way you have to
remove the template from the friend declaration:
friend ostream& operator << <> ( ostream& outStream, const
SomeClass<T, dummy>& sc );
Does this work with all compilers today? (And does it declare
all template instantiations as friend, or just the relevant
one?)
My usual solution is:
template< typename T, int dummy >
class SomeClass
{
std::ostream& print( std::ostream& dest ) const ;
// member function, no problem...
friend std::ostream& operator<<( std::ostream& dest,
SomeClass const& obj )
{
return obj.print( dest ) ;
}
// ...
} ;
By putting the implementation of the (non template) function
inline, I get a new version of it---the one I need---every time
I instantiate the class.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34