samedi 5 janvier 2019

C++11 template member ends up in an error

I am struggling with an unexpected error with my template member classes. I have two classes separated in two .h & .cc pairs. Let call them ClassA and ClassB. There is a member function in ClassA that receives a class instance from ClassB.

/** --------- classa.h -------*/
class ClassA
{
public:
   template <typename T>
   void doSomething(T* instance, const char* message);

};

 /**  ---------- classa.cc */
 template <typename T>
 void ClassA::doSomething(T* instance , const char* message)
 {
    instance->notify(message);
 }

Next pair of files is as below;

class ClassB
{
   public:
     ClassB();
     void notify(const char* message);


 };
 /**-----------------classb.cc------*/
 ClassB::ClassB()
 {
     ClassA *instance = new ClassA();
     instance->doSomething(this , "Test message");
 }

 void ClassB::notify(const char* message)
 {
    std::cout<<message;
 }

g++ doesn't compile it and returns the following error on linking.

undefined reference to `void ClassA::doSomething<ClassB>(ClassB*, char const*)'

**When I don't use template and define doSomething function as below, things go well.

void doSomething(ClassA* instance, const char* message);

Aucun commentaire:

Enregistrer un commentaire