I'm having an issue with C++ templates. Here is an explanation of what I am attempting to do, so that everyone can have a better understanding of my problem.
My framework has a base class, Component
, and users of my framework will derive Component
to create concrete Components
such as TransformComponent
and AudioComponent
. A ComponentComposite
stores a list of all the concrete Component
s that a given user has created.
I am attempting to store a list of the concrete Components
via boost::any
and boost::any_cast
s.
Below are two methods in ComponentComposite
and my list of boost::any
.
class ComponentComposite {
public:
ComponentComposite();
template<class T> bool addComponent(T* component);
template<class T> T* getComponent();
private:
QList<boost::any*>* m_components;
}
This is example code of a GameObject
, which is a ComponentComposite
. I am trying to add two Component
s to the GameObject
, and I am then trying to access the two Component
s that were added. Doing such will be common use-cases for ComponentComposite
.
GameObject::GameObject() : ComponentComposite()
{
addComponent<Components::AudioComponent>(new Components::AudioComponent());
addComponent<Components::TransformComponent>(new Components::TransformComponent());
Components::TransformComponent* transform= getComponent<Components::TransformComponent>();
Components::AudioComponent* audio= getComponent<Components::AudioComponent>();
}
Doing this proceeds to throw four errors (one for each function call):
...undefined reference to `bool BalaurEngine::Composites::ComponentComposite::addComponent<BalaurEngine::Components::AudioComponent>(BalaurEngine::Components::AudioComponent*)'
...undefined reference to `bool BalaurEngine::Composites::ComponentComposite::addComponent<BalaurEngine::Components::TransformComponent>(BalaurEngine::Components::TransformComponent*)'
...undefined reference to `BalaurEngine::Components::TransformComponent* BalaurEngine::Composites::ComponentComposite::getComponent<BalaurEngine::Components::TransformComponent>()'
...undefined reference to `BalaurEngine::Components::AudioComponent* BalaurEngine::Composites::ComponentComposite::getComponent<BalaurEngine::Components::AudioComponent>()'
If anyone would like, I can post the source code for my methods template<class T> bool addComponent(T* component);
and template<class T> T* getComponent();