R
Ruben Campos
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.
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.