Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Friend function of a template class

Reply
Thread Tools

Friend function of a template class

 
 
Ruben Campos
Guest
Posts: n/a
 
      10-28-2004
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 MyFriendFunction (MyClass <T> * const ptrMyClass);
// ...
};

#include "MyClass.cpp"


// MyClass.cpp
// ...
template <typename T>
void
MyFriendFunction (MyClass <T> * const ptrMyClass)
{
// ...
}
// ...

// Main.cpp
#include "MyClass.hpp"

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

MyFriendFunction(&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
MyFriendFunction (...) symbol. I've tried two alternative ways:

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

b) Including the MyFriendFunction 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.


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      10-28-2004
Ruben Campos wrote in news:clqeku$rb1$(E-Mail Removed) 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
MyFriendFunction (MyClass <T> * const ptrMyClass);

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


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

Note the <>.

> // ...
> };
>
> #include "MyClass.cpp"
>
>
> // MyClass.cpp
> // ...
> template <typename T>
> void
> MyFriendFunction (MyClass <T> * const ptrMyClass)
> {
> // ...
> }
> // ...
>
> // Main.cpp
> #include "MyClass.hpp"
>
> int
> main (int argn, char ** argv)
> {
> MyClass <float> x;
>
> MyFriendFunction(&x);
>
> return 0;
> }
>


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


Yep, you were declaring the friend function as a non-template.

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


Your #include "MyClass.cpp" does exactly that.

> b) Including the MyFriendFunction 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/
 
Reply With Quote
 
 
 
 
Ruben Campos
Guest
Posts: n/a
 
      10-28-2004
I've tried your solution. The linker undefined external error don't appear
now, and all works fine. I supposed that MyFriendFunction 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" <(E-Mail Removed)> escribi en el mensaje
news:Xns95906E2393D5DukcoREMOVEfreenetrtw@130.133. 1.4...
> Ruben Campos wrote in news:clqeku$rb1$(E-Mail Removed) 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
> MyFriendFunction (MyClass <T> * const ptrMyClass);
>
>> // MyClass.hpp
>> template <typename T>
>> class MyClass
>> {
>> // ...
>> friend void MyFriendFunction (MyClass <T> * const ptrMyClass);

>
> friend void MyFriendFunction<> (MyClass <T> * const ptrMyClass);
>
> Note the <>.
>
>> // ...
>> };
>>
>> #include "MyClass.cpp"
>>
>>
>> // MyClass.cpp
>> // ...
>> template <typename T>
>> void
>> MyFriendFunction (MyClass <T> * const ptrMyClass)
>> {
>> // ...
>> }
>> // ...
>>
>> // Main.cpp
>> #include "MyClass.hpp"
>>
>> int
>> main (int argn, char ** argv)
>> {
>> MyClass <float> x;
>>
>> MyFriendFunction(&x);
>>
>> return 0;
>> }
>>

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

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

>
> Your #include "MyClass.cpp" does exactly that.
>
>> b) Including the MyFriendFunction 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/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friend template function of template class Евгений Щербина C++ 1 03-19-2012 12:11 PM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
Problem with defining template friend function of a template class. PengYu.UT@gmail.com C++ 2 11-09-2005 08:27 PM
using friend function template in class template Yueh-Wei Hu C++ 0 05-23-2004 11:36 AM



Advertisments