471,573 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

Friend Function in Template Class

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!
Jul 22 '05 #1
5 2551
"Trevor Lango" <tm*****@member.aiche.org> wrote...
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
Jul 22 '05 #2
Trevor Lango wrote:
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<>( 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>
void someFunction( const TestClass< T > *someTestClass )
{
}
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

--
Reverse domain name to reply.

Jul 22 '05 #3

"Trevor Lango" <tm*****@member.aiche.org> wrote in message
news:bc*****************@newssvr27.news.prodigy.co m...
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;
Jul 22 '05 #4

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:NU****************@news-binary.blueyonder.co.uk...

"Trevor Lango" <tm*****@member.aiche.org> wrote in message
news:bc*****************@newssvr27.news.prodigy.co m...
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;


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

friend ostream &operator<<( ostream &, const someClass * );
Jul 22 '05 #5

"Trevor Lango" <tm*****@member.aiche.org> wrote in message
news:Xi*****************@newssvr25.news.prodigy.co m...

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:NU****************@news-binary.blueyonder.co.uk...

"Trevor Lango" <tm*****@member.aiche.org> wrote in message
news:bc*****************@newssvr27.news.prodigy.co m...
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;


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.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christophe Barbe | last post: by
2 posts views Thread by Christophe Barbe | last post: by
1 post views Thread by Dmitry D | last post: by
6 posts views Thread by Adam Parkin | last post: by
3 posts views Thread by 胡岳偉(Yueh-Wei Hu) | last post: by
5 posts views Thread by Ruben Campos | last post: by
21 posts views Thread by H9XLrv5oXVNvHiUI | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.