I am going to get right to the point:
//ComponentHolder.h
template<class Holder, uint ID>
class TemplateComponentHolder : public ComponentHolderInterface {
protected:
std::vector<ComponentType*> mComponents;
public:
TemplateComponentHolder() : ComponentHolderInterface(ID) {}
static const uint getStaticID() { return ID; }
};
class ConcereteComponentHolder1 : public TemplateClassHolder<ComponentType, 1000> {
public:
inline void print() { std::cout << "test"; }
};
//World.h
class World {
private:
std::map<uint, ComponentHolderInterface*> mHolders;
public:
template<class Holder> Holder * getHolder() {
auto i = mHolders.find(Holder::getStaticID());
if(i != mHolders.end())
return static_cast<Holder*>((*i));
return NULL;
}
/* ... */
};
//Main code
int main() {
World * world = new World;
world->addHolder(new ConcerteComponentHolder1);
world->getHolder<ConcreteComponentHolder1>()->print();
}
I get unresolved external symbol error. Says cannot resolve "ConcereteComponentHolder1::ID". If I change the static variable to non const and add it to a source file:
//ComponentHolder.cpp
uint ConcreteComponentHolder1::ID = 1000;
There is no problem. It makes sense why the latter one must be defined explicitly. But when I am using const
, I have to define it in the header. Getting a linker error when using const just doesn't make sense. Is it because of the template function being generated in the header? Or is it something else?