C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Friend function of a template class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Ruben Campos
Guest





PostPosted: Thu Oct 28, 2004 9:37 am    Post subject: Friend function of a template class Reply with quote



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.


Back to top
Rob Williscroft
Guest





PostPosted: Thu Oct 28, 2004 9:50 am    Post subject: Re: Friend function of a template class Reply with quote



Ruben Campos wrote in news:clqeku$rb1$1 (AT) peque (DOT) uv.es in comp.lang.c++:

Quote:
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);

Quote:
// MyClass.hpp
template <typename T
class MyClass
{
// ...
friend void MyFriendFunction (MyClass

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

Note the <>.

Quote:
// ...
};

#include "MyClass.cpp"


// MyClass.cpp
// ...
template <typename T
void
MyFriendFunction (MyClass {
// ...
}
// ...

// 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.

Quote:
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.

Quote:
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/

Back to top
Ruben Campos
Guest





PostPosted: Thu Oct 28, 2004 3:04 pm    Post subject: Re: Friend function of a template class Reply with quote



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" <rtw (AT) freenet (DOT) co.uk> escribió en el mensaje
news:Xns95906E2393D5DukcoREMOVEfreenetrtw (AT) 130 (DOT) 133.1.4...
Quote:
Ruben Campos wrote in news:clqeku$rb1$1 (AT) peque (DOT) 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 void
MyFriendFunction (MyClass
// MyClass.hpp
template <typename T
class MyClass
{
// ...
friend void MyFriendFunction (MyClass
friend void MyFriendFunction<> (MyClass <T> * const ptrMyClass);

Note the <>.

// ...
};

#include "MyClass.cpp"


// MyClass.cpp
// ...
template <typename T
void
MyFriendFunction (MyClass {
// ...
}
// ...

// 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/



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.