C++:无法将参数 1 从子非模板 class 转换为父模板 class
C++: Cannot convert argument 1 from Child non-template class to Parent template class
我知道这里有很多这样的 "cannot convert argument" 问题,但我保证我看了很多,我认为我的特殊情况还没有被问到,而且我已经尝试调试了很长时间几个小时,但似乎无法得到它。
基本上,我正在为 2D 游戏引擎创建组件系统。我将我的逻辑分为两部分:Component
和 ComponentSystem
。 Component
class 将仅存储数据,而 ComponentSystem
将相应地更新和操作该数据。
ComponentSystem
基础 class 是一个抽象的模板,用于指定系统将使用哪个 Component
。这是 .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;
};
然后我还有一个 SpriteComponentSystem
,它派生自 ComponentSystem
,但模板类型为 SpriteComponent
:
class SpriteComponentSystem : public ComponentSystem<SpriteComponent>
{
public:
SpriteComponentSystem();
~SpriteComponentSystem();
virtual void UpdateComponents(float dt);
virtual void PopulateFromCurrentLevel();
virtual void ClearFromCurrentLevel();
};
最后,在我的 Game
class 中,我正在存储基数 class 的向量,如下所示:
std::vector<ComponentSystem<Component*>*> systems;
但是,当我尝试将 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 &'
我已经尝试实现从一个对象到另一个对象的特定转换,还尝试执行一个 dynamic_cast
,它在编译时有效,但该对象在运行时 nullptr
。
万一有人想知道,SpriteComponent
确实继承自 Component
,这是基础 class。再次感谢大家的帮助!
首先,您正在初始化一个 std::vector<ComponentSystem<Component*>*>
,它接受一个带有 Component*
模板参数的 ComponentSystem
。这意味着包含在 ComponentSystem
中的向量 components
持有指向 Component
的指针。这可能是你的一个错误,除非你真的想持有一个指向指针的指针。如果你不这样做,你的向量应该被初始化 std::vector<ComponentSystem<Component>*>
.
其次,您似乎想使用运行时多态性,以便您的向量 systems
不仅可以容纳 ComponentSystem<Component>
对象,还可以容纳 SpriteComponentSystem
(又名 ComponentSystem<SpriteComponent>
对象) .
不幸的是,语言不允许这样做,因为模板是在编译时实例化的,并且每个实例化都是它自己的类型。该语言将 ComponentSystem<Component>
和 ComponentSystem<SpriteComponent>
视为不同的类型,因此编译器会抱怨。
我认为您需要重新考虑这种设计理念。
我知道这里有很多这样的 "cannot convert argument" 问题,但我保证我看了很多,我认为我的特殊情况还没有被问到,而且我已经尝试调试了很长时间几个小时,但似乎无法得到它。
基本上,我正在为 2D 游戏引擎创建组件系统。我将我的逻辑分为两部分:Component
和 ComponentSystem
。 Component
class 将仅存储数据,而 ComponentSystem
将相应地更新和操作该数据。
ComponentSystem
基础 class 是一个抽象的模板,用于指定系统将使用哪个 Component
。这是 .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;
};
然后我还有一个 SpriteComponentSystem
,它派生自 ComponentSystem
,但模板类型为 SpriteComponent
:
class SpriteComponentSystem : public ComponentSystem<SpriteComponent>
{
public:
SpriteComponentSystem();
~SpriteComponentSystem();
virtual void UpdateComponents(float dt);
virtual void PopulateFromCurrentLevel();
virtual void ClearFromCurrentLevel();
};
最后,在我的 Game
class 中,我正在存储基数 class 的向量,如下所示:
std::vector<ComponentSystem<Component*>*> systems;
但是,当我尝试将 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 &'
我已经尝试实现从一个对象到另一个对象的特定转换,还尝试执行一个 dynamic_cast
,它在编译时有效,但该对象在运行时 nullptr
。
万一有人想知道,SpriteComponent
确实继承自 Component
,这是基础 class。再次感谢大家的帮助!
首先,您正在初始化一个 std::vector<ComponentSystem<Component*>*>
,它接受一个带有 Component*
模板参数的 ComponentSystem
。这意味着包含在 ComponentSystem
中的向量 components
持有指向 Component
的指针。这可能是你的一个错误,除非你真的想持有一个指向指针的指针。如果你不这样做,你的向量应该被初始化 std::vector<ComponentSystem<Component>*>
.
其次,您似乎想使用运行时多态性,以便您的向量 systems
不仅可以容纳 ComponentSystem<Component>
对象,还可以容纳 SpriteComponentSystem
(又名 ComponentSystem<SpriteComponent>
对象) .
不幸的是,语言不允许这样做,因为模板是在编译时实例化的,并且每个实例化都是它自己的类型。该语言将 ComponentSystem<Component>
和 ComponentSystem<SpriteComponent>
视为不同的类型,因此编译器会抱怨。
我认为您需要重新考虑这种设计理念。