简体   繁体   English

C ++:无法将参数1从子非模板类转换为父模板类

[英]C++: Cannot convert argument 1 from Child non-template class to Parent template class

I know there is a lot of these "cannot convert argument" questions here, but I promise I looked around a lot and I think my particular case hasn't been asked yet, plus I have tried debugging this for quite a few hours but can't seem to get it. 我知道这里有很多“无法转换参数”的问题,但是我保证我环顾四周,我认为尚未询问我的特殊情况,而且我已经尝试调试了好几个小时,但可以似乎没有得到。

Basically, I am creating a Component system for a 2D game engine. 基本上,我正在为2D游戏引擎创建组件系统。 I have separated my logic into two parts: Component and ComponentSystem . 我将逻辑分为两部分: ComponentComponentSystem The Component class will store only data, while the ComponentSystem will update and manipulate that data accordingly. Component类将仅存储数据,而ComponentSystem将相应地更新和操纵该数据。

The ComponentSystem base class is abstract with a template to specify which Component the system will be using. ComponentSystem基类是抽象的,带有一个模板,用于指定系统将使用哪个Component Here is the .h file: 这是.h文件:

// =================================================================================================
// == COMPONENT SYSTEM CLASS =======================================================================
// =================================================================================================
template <class T>
class ComponentSystem
{
public:
    // -- ctor & dtor
    ComponentSystem<T>();
    ~ComponentSystem<T>();

    // -- update components
    virtual void UpdateComponents(float dt) = 0;
    // -- populates the components of this systems
    virtual void PopulateFromCurrentLevel() = 0;
    // -- clears the components of this system
    virtual void ClearFromCurrentLevel() = 0;

protected:
    // -- actual list of components
    std::vector<T*> components;
};

I then also have a SpriteComponentSystem , which derives from ComponentSystem , but with the SpriteComponent as the template type: 然后,我还有一个SpriteComponentSystem ,它派生自ComponentSystem ,但以SpriteComponent作为模板类型:

class SpriteComponentSystem : public ComponentSystem<SpriteComponent>
{
public:
    SpriteComponentSystem();
    ~SpriteComponentSystem();
    virtual void UpdateComponents(float dt);
    virtual void PopulateFromCurrentLevel();
    virtual void ClearFromCurrentLevel();
};

Finally, in my Game class I am storing a vector of the base class, like so: 最后,在我的Game类中,我存储了基类的向量,如下所示:

std::vector<ComponentSystem<Component*>*> systems;

However, when I try to push a SpriteComponentSystem* into systems , I get the following error: 但是,当我尝试将SpriteComponentSystem*推入systems ,出现以下错误:

C2664 - 'void std::vector<HSZGame::ComponentSystem<HSZGame::Component *> 
*,std::allocator<_Ty> >::push_back(_Ty &&)': cannot convert argument 1 from 
'HSZGame::SpriteComponentSystem *' to 
'HSZGame::ComponentSystem<HSZGame::Component *> *const &'  

I have tried implementing a specific cast from one object to the other, and also tried doing a dynamic_cast which worked for compile time but the object was then nullptr at runtime. 我试过实现从一个对象到另一个对象的特定转换,还尝试执行dynamic_cast ,该函数在编译时有效,但该对象在运行时为nullptr

In case anyone is wondering, SpriteComponent does indeed inherit from Component , which is the base class. 万一有人怀疑, SpriteComponent确实确实继承了Component ,它是基类。 Thanks again for your help everyone! 再次感谢大家的帮助!

First of all you are initializing a std::vector<ComponentSystem<Component*>*> that takes a ComponentSystem with a Component* template parameter. 首先,要初始化std::vector<ComponentSystem<Component*>*>需要一个ComponentSystemComponent*模板参数。 This means that your vector components that is contained within ComponentSystem is holding a pointer to a pointer to a Component . 这意味着ComponentSystem包含的向量components持有指向Component的指针。 This might be an error on your part unless you actually mean to hold a pointer to a pointer. 除非您实际上打算持有指向指针的指针,否则这可能是您的错误。 If you don't your vector should be initialized std::vector<ComponentSystem<Component>*> . 如果不这样做,则应初始化std::vector<ComponentSystem<Component>*>

Secondly it seems you would like to use runtime polymorphism so that your vector systems can hold not only ComponentSystem<Component> objects but also SpriteComponentSystem (AKA ComponentSystem<SpriteComponent> objects). 其次,似乎您想使用运行时多态性,以便矢量systems不仅可以容纳ComponentSystem<Component>对象,而且还可以容纳SpriteComponentSystem (AKA ComponentSystem<SpriteComponent>对象)。

Unfortunately the language does not allow this since templates are instantiated at compile time and each instantiation is its own type. 不幸的是,该语言不允许这样做,因为模板是在编译时实例化的,并且每个实例化都是其自己的类型。 The language views ComponentSystem<Component> and ComponentSystem<SpriteComponent> as separate types and hence the compiler is complaining. 语言将ComponentSystem<Component>ComponentSystem<SpriteComponent>视为单独的类型,因此编译器会抱怨。

I think you would need to rethink this design philosophy. 我认为您需要重新考虑这种设计理念。

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

相关问题 C ++非模板类的朋友成为模板类的成员 - c++ non-template class friends to a member of template class 模板类源自C ++中非模板基类的事件系统 - Event System with Template class deriving from Non-Template base class in c++ C ++ HowTo:从抽象的专用模板继承的非模板类 - C++ HowTo: Non-template class inheriting from an abstract specialized template C ++保留了一个指向模板对象的指针的集合,这些指针均来自非模板类 - C++ keeping a collection of pointers to template objects, all derived from a non-template class C ++:非模板委托类上的性能提高 - c++: Performance increase on non-template delegate class 非模板类中任何类型的C ++成员变量 - C++ member variable of any type in non-template class 当我将带有非模板类的模板类放在同一cpp文件中时出现链接错误-C ++ - Link error when I put template class with non-template classes in same cpp file - C++ C ++标准是否允许非模板类的模板构造函数? - Does the C++ standard allow a template constructor for a non-template class? 在c ++中的非模板类中使用成员模板函数是否合适? - Is it good to have a member template function inside a non-template class in c++? C ++符号范围搜索顺序与模板和非模板类不同? - C++ symbol scope search order different for template and non-template class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM