繁体   English   中英

异常重复的模板模式非法调用非静态成员函数

[英]Curiously Recurring Template Pattern Illegal call of non-static member funciton

最近,我一直在尝试许多模板元编程,尤其是使用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
}

实体

class Entity {
private:
    short id;
public:
    short GetId() {return id;}
};

我没有声明任何静态函数,也不想声明。 实际上,我要求它们成为成员函数,因为它们将对实例特定的数据进行操作。

如果有人知道为什么会发生此错误,以及可能的解决方案,我将不胜感激。 提前致谢。

您知道并确保MeshComponent继承自Component<MeshComponent> ,但是编译器不知道:就Component的定义而言, Component<T>T是不相关的。 您需要显式执行向下转换:

static_cast<T*>(this)->InternalSetEntity(_entity);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM