Friend Function in Template Class

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!
 
V

Victor Bazarov

Trevor Lango said:
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 );

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.
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
Exactly.

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!

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
 
A

Adam Fineman

Trevor said:
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 );

friend void someFunction said:
public:
TestClass( );

};
#endif

//****************************************************
// end testClass.h
//****************************************************



//****************************************************
// testClass.cpp
//****************************************************
#include "testClass.h"

template< class T >
void someFunction< T >( const TestClass< T > *someTestClass )

template< class T>
void someFunction( const TestClass said:
{
}


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>

Yes, you had fixed the first issue, but not the second.

HTH,

- Adam
 
N

Nick Hounsome

Trevor Lango said:
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 );

Why would you want such a thing in this context?
Unless you have missed out some extra args the smart thing to do is just
make it a simple const member function
void someFunction() const;
 
T

Trevor Lango

Nick Hounsome said:
Why would you want such a thing in this context?
Unless you have missed out some extra args the smart thing to do is just
make it a simple const member function
void someFunction() const;

The function in question is the following (which can't be a member
function):

friend ostream &operator<<( ostream &, const someClass * );
 
N

Nick Hounsome

Trevor Lango said:
The function in question is the following (which can't be a member
function):

friend ostream &operator<<( ostream &, const someClass * );

This is the third time in 2 days that some version of this has come up and I
would do them all the same:

template <class T> class TestClass {
public: ostream& print(ostream&) const; }

inline template <class T> ostream& operator<<(ostream& os, const
TestClass<T>& t)
{ return t.print(os); }

P.S. It is never a good idea to wite an output operator that takes a
pointer - always use a const reference.

The other advantage of this approach is that just adding 'virtual' gives you
polymorphic printing.

I am unconvinced of the need for any sort of special access for printing
anyway because
I think it is questionable to output some aspect of an object that you
couldn't get from the public interface.
This is not a cast iron rule with me but consider carefully why you would
break it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top