I'm trying to access the x and y values from PositionComponent in my MovementComponent but I get this error and I just can't figure out why I get this.
in my Entity class I've got a function GetComponent() that looks like this:
template<typename ComponentType>
ComponentType* Entity::GetComponent() {
for(unsigned int i = 0; i < m_components.size(); i++) {
if(ComponentType* cmp = dynamic_cast<ComponentType*>(m_components[i])) {
return cmp;
}
}
return NULL;
}
m_components is a vector of Component* that looks like this:
std::vector<Component*> m_components;
My MovementComponent looks like this, and I get an error for some reason here:
void MovementComponent::MoveRight() {
m_ContainingEntity->GetComponent<PositionComponent>()->x++; //I get the error at this line.
}
Error: undefined reference to `PositionComponent* Entity::GetComponent()'
What did I do wrong? I've got a feeling that the solution is really simple and that I just didn't pay much attention.
Entity.h:
#ifndef __ENTITY_H__
#define __ENTITY_H__
#include <vector>
#include <cstddef>
class Component;
class ECSManager;
class Entity {
public:
void AddComponent(Component* cmp);
template <typename ComponentType>
ComponentType* GetComponent();
private:
friend class ECSManager;
std::vector<Component*> m_components;
};
#endif // __ENTITY_H__
Entity.cpp:
#include "Entity.h"
#include "Component.h"
void Entity::AddComponent(Component* cmp) {
m_components.push_back(cmp);
cmp->m_ContainingEntity = this;
}
template<typename ComponentType>
ComponentType* Entity::GetComponent() {
for(unsigned int i = 0; i < m_components.size(); i++) {
if(ComponentType* cmp = dynamic_cast<ComponentType*>(m_components[i])) {
return cmp;
}
}
return NULL;
}
MovementComponent.h:
#ifndef __MOVEMENTCOMPONENT_H__
#define __MOVEMENTCOMPONENT_H__
#include "Component.h"
class MovementComponent: public Component {
public:
void MoveRight();
void MoveLeft();
private:
};
#endif // __MOVEMENTCOMPONENT_H__
MovementComponent.cpp:
#include "MovementComponent.h"
#include "Entity.h"
#include "PositionComponent.h"
void MovementComponent::MoveRight() {
m_ContainingEntity->GetComponent<PositionComponent>()->x++;
}
void MovementComponent::MoveLeft() {
}