Posso capirlo a compile time perchè non da errore ma non capisco proprio perchè non da errore a run-time.
Inoltre approfitto per chiederti questa cosa.
Ho il file Entity.h:
codice:
#include <string>
#include <map>
#include <iostream>
#include <typeindex>
#include "IComponent.h"
using namespace std;
class Entity {
public:
template <class T> void addComponent(){
T *newComponent = new T();
this->mComponents[type_index(typeid(T))] = newComponent;
//this->mComponents.insert(pair<type_index, T*>(type_index(typeid(T)), newComponent));
newComponent->entity = this;
newComponent->init();
}
template <class T> T* getComponent(){
map<type_index, IComponent*>::iterator it =
this->mComponents.find(type_index(typeid(T)));
if (it != this->mComponents.end())
return static_cast<T*>(it->second);
else
return NULL;
}
void removeComponent(string name, IComponent *Component);
bool HasComponent(int eID);
//void fetchComponents();
map<type_index, IComponent*> mComponents;
private:
// Entity ID
int eID;
// riferimento a tutti i componenti
//map<type_index, IComponent*> mComponents;
};
come puoi notare ho implementato direttamente ne file header i metodi addComponent e getComponent, e l'ho dovuto fare dato che se li implemento in Entity.cpp mi rileva questo errore:
codice:
riferimento al simbolo esterno "public: class AI_followTarget * __thiscall Entity::getComponent<class AI_followTarget>(void)" (??$getComponent@VAI_followTarget@@@Entity@@QAEPAVAI_followTarget@@XZ) non risolto nella funzione _main