【问题标题】:Global std::vector is not retaining data全局 std::vector 不保留数据
【发布时间】:2016-05-06 01:40:15
【问题描述】:

我有一个标题...

Components.h

namespace ComponentManager
{
  void InitializeComponents(std::size_t numComponents);
  void AddComponent(const Component& c, std::size_t position);
  std::vector<Component> GetComponents();
}

...和实现:

Components.cpp

#include "Components.h"

std::vector<ComponentManager::Component> components;

void ComponentManager::InitializeComponents(std::size_t numComponents)
{
  components.resize(numComponents, Component());
}

void ComponentManager::AddComponent(const Component& c, std::size_t pos)
{
  components[pos] = c;
}

std::vector<ComponentManager::Component> ComponentManager::GetComponents()
{
  return components;
}

main.cpp中,我确保初始化了组件向量,并添加了几个组件。在调试每个 AddComponent(...) 调用后,我看到组件实际上存储在向量中。但是,当我调用 GetComponents() 时,返回的向量是不同的,就好像组件从未存储过一样。

我错过了什么?

编辑:添加 main.cpp(在 main() 函数内)

//Inside EntityManager::Initialize(), I call ComponentManager::InitializeComponents()
EntityManager::Initialize(5000);
for (unsigned int i = 0; i < 10; ++i)
{
    uint16_t handle = EntityManager::CreatePlayer(static_cast<float>(i), 50.0f * i);
    //Inside EntityManager::AddComponent, I call ComponentManager::AddComponent(..)
    EntityManager::AddComponent(handle, ComponentManager::POSITION_COMPONENT | ComponentManager::RENDERABLE_COMPONENT);
}
auto components = ComponentManager::GetComponents(); //this vector does not contain the elements that were added.

【问题讨论】:

  • 您说的是“in main.cpp”,但您实际上是指“main.cpp”中的main 函数还是某个全局对象的构造函数?因为如果是后者,那就是初始化顺序问题,而且我们最近也有完全相同的问题。
  • Componet 是什么?
  • 你为什么不把main.cpp的内容也贴出来呢?它们对于回答您的问题非常重要。
  • 这是一个可能的骗局:stackoverflow.com/questions/7542054/…
  • 我同意 mcve 时间@LightnessRacesinOrbit。我仍然想询问 ComponentManager::Component operator= 和复制构造函数实现我认为错误可能在那里

标签: c++ vector


【解决方案1】:

原来这是Component的定义:

union Component
{
    PositionComponent positionComponent;
    VelocityComponent velocityComponent;
    AccelerationComponent accelerationComponent;
    RenderableComponent renderableComponent;
    Component() {}
    ~Component() {}
    Component(const Component& other) {}
    Component& operator=(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
        return *this;
    }
};

为构造函数和复制构造函数添加定义是解决方法:

union Component
{
    PositionComponent positionComponent;
    VelocityComponent velocityComponent;
    AccelerationComponent accelerationComponent;
    RenderableComponent renderableComponent;
    Component() 
    {  
        std::memset(this, 0, sizeof(Component)); 
    }
    ~Component() {}
    Component(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
    }
    Component& operator=(const Component& other)
    {
        std::memmove(this, &other, sizeof(other));
        return *this;
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2018-10-23
    • 2017-05-27
    • 2012-12-13
    • 2012-10-19
    相关资源
    最近更新 更多