向unordered_map添加unique_ptr时'this was nullptr'

'this was nullptr' when adding unique_ptr to unordered_map

本文关键字:was nullptr this ptr map unordered 添加 unique      更新时间:2023-10-16

我正在创建一个简单的演示项目,但我无法将unique_ptr添加到无序列表中,不知何故它添加了键而不是指针,后来当我尝试在指针上调用方法时,我得到"这是nullptr错误"。

我有一个基类Scene负责存储指针并将它们添加到地图中,如下所示:

void Scene::addGameObject(const std::string& name, const GameObjectParams& params)
{
m_objects.try_emplace(name, std::make_unique<GameObject>(params));
}
bool Scene::containsGameObject(const std::string& name)
{
if (m_objects[name] != NULL)
return true;
return false;
}
GameObject& Scene::getGameObject(const std::string& name)
{
auto it = m_objects.find(name);
if (it != m_objects.end())
return *it->second;

}
void Scene::drawScene()
{
for (const auto& object : m_objects)
object.second->draw(); // <--------here the error gets thrown
}

如果我从具有scene作为成员的类调用此方法,则每个示例都可以正常工作:

//GameLayer.h
class GameLayer 
{
private:
// GameScene is derived class from Scene
GameScene m_gameScene;
};
//GameLayer.cpp
m_gameScene.addGameObject(
"player",
{
Vector3f(0.0f, 0.0f, 1.0f),
Vector3f(100.0f, 70.0f, 1.0f),
Quaternion(),
Vector4f(1.0f),
fighter
}
);

但是现在当我尝试从派生类本身添加一个新对象时,它会添加键但没有指针

void GameScene::spawnLaser(Vector3f pos)
{
for (int i = 0; i < 100; i++)
{
std::string name = std::to_string(i);
if (!this->containsGameObject(name))
{
m_lasers.emplace_back(name);
this->addGameObject(
"laser",
GameObjectParams {
Vector3f(0.0f, 0.0f, 1.0f),
Vector3f(100.0f, 70.0f, 1.0f),
Quaternion(),
Vector4f(1.0f),
nullptr
}
);
return;
}
}
}

GameObject是应使用std::make_unique创建的对象

GameObject::GameObject(const GameObjectParams& params)
: m_position(params.position.x, params.position.y, params.position.z),
m_scale(params.scale.x, params.scale.y, params.scale.z),
m_rotation(),
m_texture(params.texture),
m_color(params.color)
{
}
void GameObject::setTexture(cheetah::Texture* texture)
{
m_texture = texture;
}
void GameObject::setPosition(const cheetah::Vector3f& position)
{
m_position.x = position.x;
m_position.y = position.y;
m_position.z = position.z;
}
void GameObject::translate(const cheetah::Vector3f& position)
{
m_position.x += position.x;
m_position.y += position.y;
m_position.z += position.z;
}

void GameObject::draw()
{
// 'this was nullptr' gets thrown here
if (m_texture != nullptr)
{
Renderer2D::drawQuad(DrawTexturedQuadParams{ m_position, m_scale, m_rotation, Vector4f(1.0f), m_texture });
}
else
{
Renderer2D::drawQuad(DrawQuadParams{ m_position, m_scale, m_rotation, m_color });
}
}

似乎make_unique没有创建一个新对象,但我不知道为什么。

在此代码中:

bool Scene::containsGameObject(const std::string& name)
{
if (m_objects[name] != NULL)
return true;
return false;
}

如果地图中不存在name,则地图的operator[]将使用默认构造的std::unique_ptr添加name,该包含nullptr。 您需要使用m_objects.find(name)而不是m_objects[name]

bool Scene::containsGameObject(const std::string& name) const
{
return (m_objects.find(name) != m_objects.end());
}

在此代码中:

GameObject& Scene::getGameObject(const std::string& name)
{
auto it = m_objects.find(name);
if (it != m_objects.end())
return *it->second;

}

如果在映射中找不到name,则返回值不确定。 您需要:

返回
  • 指针而不是引用,以便在找不到name时可以返回nullptr(然后更新调用方以检查nullptr(:
GameObject* Scene::getGameObject(const std::string& name)
{
auto it = m_objects.find(name);
if (it != m_objects.end())
return it->second.get();
return nullptr;
}
  • 继续返回引用,但如果未找到name则引发异常:
GameObject& Scene::getGameObject(const std::string& name)
{
// std::unordered_map::at() throws std::out_of_range if the key is not found...
return *(m_objects.at(name)->second);
}