[英]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
. 我将逻辑分为两部分:
Component
和ComponentSystem
。 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*>*>
需要一个ComponentSystem
与Component*
模板参数。 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.