473,699 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend function of a template class

I have a problem with a template function that is declared as a friend of a
template class. I'll first show the exact problem with source code:

// MyClass.hpp
template <typename T>
class MyClass
{
// ...
friend void MyFriendFunctio n (MyClass <T> * const ptrMyClass);
// ...
};

#include "MyClass.cp p"
// MyClass.cpp
// ...
template <typename T>
void
MyFriendFunctio n (MyClass <T> * const ptrMyClass)
{
// ...
}
// ...

// Main.cpp
#include "MyClass.hp p"

int
main (int argn, char ** argv)
{
MyClass <float> x;

MyFriendFunctio n(&x);

return 0;
}

Although MyClass <T> is a template, I still prefer to place the
implementation of its methods in a MyClass.cpp file, and not to show them
with the class declaration. But because MyClass <T> is a template, and so it
needs method definitions visible in its header file, I include MyClass.cpp
file inside MyClass.hpp file instead of building it (I don't compile
MyClass.cpp). I've tried before this file scheme, and it works fine.

Trying to build this, I receive an linker undefined external error for the
MyFriendFunctio n (...) symbol. I've tried two alternative ways:

a) Including the MyFriendFunctio n definition directly into MyClass.hpp,
after (outside) class declaration. This returns the same linker error.

b) Including the MyFriendFunctio n implementation directly into declaration,
into MyClass.hpp inside class declaration. This works fine and don't return
any error.

But I don't want function implementations inside class declaration, or
merely inside a header file. Can you help me with this? Thank you very much
in advance.
Jul 22 '05 #1
2 3982
Ruben Campos wrote in news:cl******** **@peque.uv.es in comp.lang.c++:
I have a problem with a template function that is declared as a friend
of a template class. I'll first show the exact problem with source
code:

/* Forward Declaration's
*/

template <typename T>
class MyClass;

template <typename T>
void
MyFriendFunctio n (MyClass <T> * const ptrMyClass);
// MyClass.hpp
template <typename T>
class MyClass
{
// ...
friend void MyFriendFunctio n (MyClass <T> * const ptrMyClass);
friend void MyFriendFunctio n<> (MyClass <T> * const ptrMyClass);

Note the <>.
// ...
};

#include "MyClass.cp p"
// MyClass.cpp
// ...
template <typename T>
void
MyFriendFunctio n (MyClass <T> * const ptrMyClass)
{
// ...
}
// ...

// Main.cpp
#include "MyClass.hp p"

int
main (int argn, char ** argv)
{
MyClass <float> x;

MyFriendFunctio n(&x);

return 0;
}

Trying to build this, I receive an linker undefined external error for
the MyFriendFunctio n (...) symbol. I've tried two alternative ways:

Yep, you were declaring the friend function as a non-template.
a) Including the MyFriendFunctio n definition directly into
MyClass.hpp, after (outside) class declaration. This returns the same
linker error.

Your #include "MyClass.cp p" does exactly that.
b) Including the MyFriendFunctio n implementation directly into
declaration, into MyClass.hpp inside class declaration. This works
fine and don't return any error.


Yes, its the only way to give the non-template declaration a defenition.

Also your are right to want to avoid this as it often leads to problems:

template < typename U, typename V >
struct X
{
friend void f( U * ) {};
};

X< int, int > xii;
/* Ok */

X< int, short > xis;
/* Whoopse: A defenition for f( int * ) has already been given */

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
I've tried your solution. The linker undefined external error don't appear
now, and all works fine. I supposed that MyFriendFunctio n was a template
function due to its declaration inside a template class, but I was obviously
wrong.

Thank you very much for your help.
"Rob Williscroft" <rt*@freenet.co .uk> escribió en el mensaje
news:Xn******** *************** ***********@130 .133.1.4...
Ruben Campos wrote in news:cl******** **@peque.uv.es in comp.lang.c++:
I have a problem with a template function that is declared as a friend
of a template class. I'll first show the exact problem with source
code:


/* Forward Declaration's
*/

template <typename T>
class MyClass;

template <typename T>
void
MyFriendFunctio n (MyClass <T> * const ptrMyClass);
// MyClass.hpp
template <typename T>
class MyClass
{
// ...
friend void MyFriendFunctio n (MyClass <T> * const ptrMyClass);


friend void MyFriendFunctio n<> (MyClass <T> * const ptrMyClass);

Note the <>.
// ...
};

#include "MyClass.cp p"
// MyClass.cpp
// ...
template <typename T>
void
MyFriendFunctio n (MyClass <T> * const ptrMyClass)
{
// ...
}
// ...

// Main.cpp
#include "MyClass.hp p"

int
main (int argn, char ** argv)
{
MyClass <float> x;

MyFriendFunctio n(&x);

return 0;
}


Trying to build this, I receive an linker undefined external error for
the MyFriendFunctio n (...) symbol. I've tried two alternative ways:


Yep, you were declaring the friend function as a non-template.
a) Including the MyFriendFunctio n definition directly into
MyClass.hpp, after (outside) class declaration. This returns the same
linker error.


Your #include "MyClass.cp p" does exactly that.
b) Including the MyFriendFunctio n implementation directly into
declaration, into MyClass.hpp inside class declaration. This works
fine and don't return any error.


Yes, its the only way to give the non-template declaration a defenition.

Also your are right to want to avoid this as it often leads to problems:

template < typename U, typename V >
struct X
{
friend void f( U * ) {};
};

X< int, int > xii;
/* Ok */

X< int, short > xis;
/* Whoopse: A defenition for f( int * ) has already been given */

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12530
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
10158
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
4443
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
5
2715
by: Trevor Lango | last post by:
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
6
2333
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
2089
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
2635
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
4735
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
8697
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
8622
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
9045
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
8930
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
8892
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...
1
6538
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4382
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.