首页 > 解决方案 > 创建不同模板化对象的模板化数组的模板化数组

问题描述

嘿,所以我目前正在研究一个非常简单的 ecs,我想将我Components的所有 (其中有不同类型的)存储在数组中,其中也有不同的类型,并将所有这些数组存储在一个更大的数组中。像这样:

ComponentArray< ComponentArray< Component > > components;

现在我读到了这个,显然我必须ComponentArray为以及创建一个基类,Component并让它们MainArray具有指向基类的指针。所以我认为声明应该是这样的:

ComponentArray< ComponentArrayBase< ComponentBase* >* > components;

并且在另一个实现文件的初始化中(因为它是一个静态类并且具有模板成员函数,所以我必须使实现成为一个.inl文件并且不能在那里初始化数组):

ComponentArray< ComponentArrayBase< ComponentBase* >* > ComponentManager::components{/*creating some arrays here*/};

我的 Array 类如下所示:

template<typename ComponentType>
struct ComponentArrayBase{};

template<typename ComponentType>
struct ComponentArray : ComponentArrayBase<ComponentType>
{
    ComponentType array[maxEntitys];

    ComponentType& operator[](unsigned int index)
    {
        return array[index];
    }
};

我的Components样子是这样的:

struct ComponentBase{};

struct PositionComponent : ComponentBase
{
    //some member variables
};

我想在哪里使用它,例如:

template<typename ComponentType>
ComponentType& ComponentManager::getComponent(Entity e, unsigned char index)
{
    return *(*(components[index])[e]);
}

编译时出现以下错误:

error: no match for ‘operator*’ (operand type is ‘ComponentArrayBase<ComponentBase*>’)
     return *(*(components[componentArray])[e]);

我不确定我所说的是否都有意义,所以非常感谢您的回答

标签: c++templatesarrayofarrays

解决方案


推荐阅读