T
Trevor Lango
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 );
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
testClass.h:10: warning: (if this is not what you intended, make sure
testClass.h:10: warning: the function template has already been declared,
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 the
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 appreciated.
I am uncertain as to how to use friend functions correctly within template
classes, any clarification(s) are welcomed!
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 );
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
testClass.h:10: warning: (if this is not what you intended, make sure
testClass.h:10: warning: the function template has already been declared,
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 the
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 appreciated.
I am uncertain as to how to use friend functions correctly within template
classes, any clarification(s) are welcomed!