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@freenet.co.uk> escribió en el mensaje
news:Xns95906E2393D5DukcoREMOVEfreenetrtw@130.133. 1.4...[color=blue]
> Ruben Campos wrote in news:clqeku$rb1$1@peque.uv.es in comp.lang.c++:
>[color=green]
>> 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:
>>[/color]
>
> /* Forward Declaration's
> */
>
> template <typename T>
> class MyClass;
>
> template <typename T>
> void
> MyFriendFunction (MyClass <T> * const ptrMyClass);
>[color=green]
>> // MyClass.hpp
>> template <typename T>
>> class MyClass
>> {
>> // ...
>> friend void MyFriendFunction (MyClass <T> * const ptrMyClass);[/color]
>
> friend void MyFriendFunction<> (MyClass <T> * const ptrMyClass);
>
> Note the <>.
>[color=green]
>> // ...
>> };
>>
>> #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;
>> }
>>[/color]
>[color=green]
>>
>> Trying to build this, I receive an linker undefined external error for
>> the MyFriendFunction (...) symbol. I've tried two alternative ways:
>>[/color]
>
> Yep, you were declaring the friend function as a non-template.
>[color=green]
>> a) Including the MyFriendFunction definition directly into
>> MyClass.hpp, after (outside) class declaration. This returns the same
>> linker error.
>>[/color]
>
> Your #include "MyClass.cpp" does exactly that.
>[color=green]
>> b) Including the MyFriendFunction implementation directly into
>> declaration, into MyClass.hpp inside class declaration. This works
>> fine and don't return any error.[/color]
>
> 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/[/color]