473,581 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(co nst
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 2693
"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(co nst
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(TestCl ass<T> const*); // func decl
template<class T> class TestClass
{
friend void someFunc<T>(Tes tClass 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(co nst
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******** *********@newss vr27.news.prodi gy.com...
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***@blueyond er.co.uk> wrote in message
news:NU******** ********@news-binary.blueyond er.co.uk...

"Trevor Lango" <tm*****@member .aiche.org> wrote in message
news:bc******** *********@newss vr27.news.prodi gy.com...
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******** *********@newss vr25.news.prodi gy.com...

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

"Trevor Lango" <tm*****@member .aiche.org> wrote in message
news:bc******** *********@newss vr27.news.prodi gy.com...
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<<(ostr eam& 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12512
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template <class T> class test{ test(void); ~test(void); friend bool operator== <> (const test<T> &p1, const test<T> &p2); }
2
10145
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o...
1
4431
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix...
6
2326
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
3
2073
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
5
2624
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
4
774
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
2
1680
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void method(foo<Tl, foo<Ur); // But VC2008 requires this
21
4679
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of...
0
7788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8299
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8163
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1397
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.