c++ - type_info 尝试引用已删除的函数

标签 c++ c++11

我正在尝试存储对象的集合(所有对象都继承自基础 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/

相关文章:

c++ - 在打开 vsync 的情况下执行 glTexImage2D 时出现奇怪的延迟

c++ - WinSock 无缘无故返回 SOCKET_ERROR

c++ - double 和 NaN 的比较结果是什么?

C++11 - 模板、友元、decltype 和访问修饰符

c++ - 我的链表反转递归方法代码有什么问题?

c++ - 为什么赋值运算符一开始就返回任何东西?

C++ 在二维数组中保存位置

c++ - 使用 for 循环创建平行四边形

c++ - boost::optional - 将 boost::in_place 用于构造函数通过引用获取其他对象的对象

c++ - 如何将秒转换为hh :mm:ss.毫秒格式c++?