I am trying to implement a way to register a class ("Car" in the code) and then using the Factory pattern, instantiate an object at run time (the base class is "Transport" and "Car" inherits from it).
This is my code in the Registry.h
#define REGISTER_CLASS(NAME, TYPE, BASE) static Register<BASE> registrer((const char*)NAME, [](void) -> BASE* { return new TYPE();});
template <class TClass>
class Register {
public:
Register<TClass>(const char* className, std::function<TClass* (void)> classFactoryFunction);
};
and this is the Registy.cpp
template <class TClass>
Register<TClass>::Register(const char* className, std::function<TClass* (void)> classFactoryFunction){
// Register the class factory function
Factory::Instance()->RegisterFactoryFunction(className, classFactoryFunction);
}
In the Car.cpp I use the following code to register the Car:
REGISTER_CLASS((const char*)"Car", Car, Transport);
If I don't use the template, all my code works... With the template, I got the following error:
sketch/src/Car.cpp.o: In function _GLOBAL__sub_I__ZN10CarC2Ev': sketch/src/Car.cpp:42: undefined reference to
Register::Register(char const*, std::function)' collect2: error: ld returned 1 exit status
What's wrong in my code?
Aucun commentaire:
Enregistrer un commentaire