473,549 Members | 2,616 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 3964
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
12509
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
10140
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
4429
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...
5
2689
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
2325
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
2068
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
2622
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
1675
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
4670
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
7532
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...
0
7730
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
7975
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
7492
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
6059
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...
1
5381
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
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.