So after studying the EntityX source code extensively I finally figured out how to use the varidic templates. But there is one problem now, which happens if I try to attach an component outside of the entity class. Let me show you how the AttachComponent()-method is set up in Entity.h:
#pragma once
#include "Components.h"
using namespace std;
class Entity
{
typedef vector<BaseComponent*> componentVector;
public:
Entity(void);
virtual ~Entity(void);
template<typename C, typename... Args>
void AttachComponent(Args&& ... args);
private:
componentVector m_vComponents;
};
in Entity.cpp
#include "Entity.h"
template<typename C, typename ... Args>
void Entity::AttachComponent(Args&& ... args)
{
BaseComponent* pComponent = new C(args...);
m_vComponents.push_back(pComponent);
}
now the component itself in Component.h:
#pragma once
struct BaseComponent
{
typedef int Family;
protected:
static Family family_count;
};
template <typename Derived>
struct Component : public BaseComponent {
static Family family(void);
};
template<typename C>
BaseComponent::Family Component<C>::family(void) {
static BaseComponent::Family Family = family_count++;
return Family;
}
And my "PositionComponent" in Components.h:
struct PositionComponent : Component<PositionComponent>
{
PositionComponent(int x, int y) { m_x = x; m_y = y; };
int m_x, m_y;
};
Now everything works fine if I call "AttachComponent<PositionComponent>(1, 1)" in the constructor of Entity, or any other functions of entity. As soon as I create an actual entity and call "entity.AttachComponent<PositionComponent>(1, 1)" I get an linker error:
error LNK2019: unresolved external symbol "public: void __thiscall Entity::AttachComponent<struct PositionComponent,int,int>(int &&,int &&)" (??$AttachComponent@UPositionComponent@@HH@Entity@@QAEX$$QAH0@Z) referenced in function "public: virtual void __thiscall SceneEditor::Show(void)" (?Show@SceneEditor@@UAEXXZ)
Any ideas why this is happening and how I can resolve it? Ok, I got it. I actually had to define the AttachComponent<>()-function in the header instead of the cpp-file. I'd still be happy if someone explained to me why this was necessary? Is it a thing I generally need to do with templates? Or is it just a variadic-template-thingy? Or is it a bug from the CTP_Nov2012 toolset?