| re: Friend Function in Template Class
"Trevor Lango" <tmlango@member.aiche.org> wrote...[color=blue]
> What is the appropriate syntax for placing a friend function that includes
> as one of it's parameters a pointer to the class object itself within the
> template class?
>
> I have the following:
>
> //************************************************** **
> // testClass.h
> //************************************************** **
> #ifndef TESTCLASS_H
> #define TESTCLASS_H
>
> template< class T >
> class TestClass
> {
> friend void someFunction( const TestClass< T > *someTestClass );[/color]
If your function is a template, you need to declare it before first
use. Otherwise this 'friend' declaration becomes a declaration of
a function that is not a template. IIRC.
[color=blue]
>
> public:
> TestClass( );
>
> };
> #endif
>
> //************************************************** **
> // end testClass.h
> //************************************************** **
>
>
>
> //************************************************** **
> // testClass.cpp
> //************************************************** **
> #include "testClass.h"
>
> template< class T >
> void someFunction< T >( const TestClass< T > *someTestClass )
> {
> }
>
> template< class T >
> TestClass< T >::TestClass( )
> {
> }
>
> //************************************************** **
> // end testClass.cpp
> //************************************************** **
>
>
> The following are the warnings/error(s) produced by the compiler:
>
> U:\test>g++ -c testClass.cpp
> In file included from testClass.cpp:1:
> testClass.h:10: warning: friend declaration `void someFunction(const
> TestClass<T> *)'
> testClass.h:10: warning: declares a non-template function[/color]
Exactly.
[color=blue]
> testClass.h:10: warning: (if this is not what you intended, make sure
> testClass.h:10: warning: the function template has already been[/color]
declared,[color=blue]
> testClass.h:10: warning: and add <> after the function name here)
> testClass.h:10: warning: -Wno-non-template-friend disables this warning.
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>
> Following the compiler's suggestion removes the warning but still yields[/color]
the[color=blue]
> following error:
>
> U:\test>g++ -Wno-non-template-friend -c testClass.cpp
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>
>
> Any suggestions/comments as to how to correct this will be much[/color]
appreciated.[color=blue]
> I am uncertain as to how to use friend functions correctly within template
> classes, any clarification(s) are welcomed![/color]
It's a bit tricky, and both declarations are better put in the same
header, to form this sequence:
template<class T> class TestClass; // forward declaration
template<class T> void someFunc(TestClass<T> const*); // func decl
template<class T> class TestClass
{
friend void someFunc<T>(TestClass const*); // now it uses the func
// declared above
};
...
Victor |