【发布时间】:2018-02-23 10:25:06
【问题描述】:
我最近尝试了很多模板元编程,尤其是使用 CRTP,但遇到了名义上的错误。具体错误 C2352 'MeshComponent::InternalSetEntity': 非法调用非静态成员函数。
我的代码的最小、完整和可验证的片段如下:
组件.h
class Entity //Forward declaration
template<class T, EventType... events>
class Component {
private:
short entityID;
public:
void SetEntity(Entity& _entity) { entityID = _entity.GetId(); T::InternalSetEntity(_entity); }
protected:
void InternalSetEntity(Entity& _entity) {}
};
MeshComponent.h
#include "Component.h"
class MeshComponent : public Component<MeshComponent> {
friend class Component<MeshComponent>;
protected:
void InternalSetEntity(Entity& _entity);
};
MeshComponent.cpp
#include "MeshComponent.h"
void MeshComponent::InternalSetEntity(Entity& _entity) {
//Nothing yet
}
Entity.h
class Entity {
private:
short id;
public:
short GetId() {return id;}
};
我没有声明任何静态函数,也不想声明。事实上,我要求这些是成员函数,因为它们将对实例特定的数据进行操作。
如果有人知道为什么会发生此错误以及问题的可能解决方案,我将不胜感激。提前致谢。
【问题讨论】:
标签: c++ templates static c++14 crtp