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. 2 3886
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
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/
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" <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 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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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:
...
|
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...
|
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.
...
|
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>...
|
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};
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |