473,729 Members | 2,176 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 2718
"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
12534
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
10160
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 foo or
1
4445
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 this? Thanks, Dmitry
6
2336
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
2093
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
2639
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
1690
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
4739
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 behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8925
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9288
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9206
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9154
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8156
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3240
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.