Curiously Recurring Template Pattern 非静态成员函数的非法调用
Curiously Recurring Template Pattern Illegal call of non-static member funciton
我最近尝试了很多模板元编程,尤其是使用 CRTP 时,遇到了名义上的错误。具体错误C2352'MeshComponent::InternalSetEntity':非法调用非静态成员函数。
我的代码的一个最小的、完整的和可验证的片段是这样的:
Component.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) {}
};
网格Component.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;}
};
我没有声明任何静态函数,我也不想声明。事实上,我要求它们是成员函数,因为它们将对特定于实例的数据进行操作。
如果有人知道为什么会出现此错误以及问题的可能解决方案,我将不胜感激。提前致谢。
您知道并确保 MeshComponent
继承自 Component<MeshComponent>
,但编译器不知道:据它在 Component
、[=14 的定义中所知=] 和 T
无关。您需要明确执行向下转换:
static_cast<T*>(this)->InternalSetEntity(_entity);
我最近尝试了很多模板元编程,尤其是使用 CRTP 时,遇到了名义上的错误。具体错误C2352'MeshComponent::InternalSetEntity':非法调用非静态成员函数。
我的代码的一个最小的、完整的和可验证的片段是这样的:
Component.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) {}
};
网格Component.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;}
};
我没有声明任何静态函数,我也不想声明。事实上,我要求它们是成员函数,因为它们将对特定于实例的数据进行操作。
如果有人知道为什么会出现此错误以及问题的可能解决方案,我将不胜感激。提前致谢。
您知道并确保 MeshComponent
继承自 Component<MeshComponent>
,但编译器不知道:据它在 Component
、[=14 的定义中所知=] 和 T
无关。您需要明确执行向下转换:
static_cast<T*>(this)->InternalSetEntity(_entity);