我正在尝试存储对象的集合(所有对象都继承自基础 acorn::Component
)。
但是,在我的 AddComponent
函数中,我不断收到此错误:
错误 C2280:“type_info::type_info(const type_info &)”:尝试引用已删除的函数
我不确定为什么会收到此错误,因为我的 Component
类或存储所有 Component
对象的类中没有任何已删除的函数.
这是给我带来问题的函数。它应该将 Component
添加到 map 中:
template<typename T>
void AddComponent(T* component)
{
#ifdef _DEBUG
SDL_Log("acorn::Entity::AddComponent called!");
#endif
std::pair<std::type_index, acorn::Component*> myPair = std::make_pair(typeid(T), component);
mComponentMap.insert(myPair);
}
这里是组件类:
namespace acorn
{
struct Component
{
};
struct PositionComponent : public acorn::Component
{
SDL_Rect positionRect;
float x;
float y;
uint32_t ID;
PositionComponent() : x(0.0f), y(0.0f), ID(0)
{
}
};
struct VelocityComponent : public acorn::Component
{
float xVel;
float yVel;
uint32_t ID;
VelocityComponent() : xVel(0.0f), yVel(0.0f), ID(1)
{
}
};
struct SpriteComponent : public acorn::Component
{
SDL_Rect spriteRect;
SDL_Texture* sprite;
uint32_t ID;
SpriteComponent() : ID(0)
{
spriteRect.x = 0;
spriteRect.y = 0;
spriteRect.h = 32;
spriteRect.w = 32;
sprite = nullptr;
}
};
}
我唯一能想到的是这与结构有关,但我找不到任何证实这一点的东西。有什么想法吗?
最佳答案
make_pair
不适用于不可复制类型,因为它会创建一个新的纯右值对。
替换
std::pair<std::type_index, acorn::Component*> myPair = std::make_pair(typeid(T), component);
与
std::pair<std::type_index, acorn::Component*> myPair(typeid(T), component);
你应该没问题。
关于c++ - type_info 尝试引用已删除的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257531/