Hi there, I'm encountering an error when I'm trying to make a EntityManager for an Entity class. My problem arises when I'm trying to return the second value of a map (data). Here's my code:

EntityManager.hpp:

#include <iostream>
#include <SFML/Graphics.hpp>
#include <map>
#include <memory>

#include "CEntity.hpp"

class CEntityManager
{
public:
    CEntityManager();
    ~CEntityManager();

    void createEntity(const std::string&, CEntity::Entity_Type);
    void deleteEntity(const std::string&);

    std::unique_ptr<CEntity> operator() (const std::string&);

private:
    std::map<std::string, std::unique_ptr<CEntity> > m_entities;
};

EntityManager.cpp:

#include "CEntityManager.hpp"

CEntityManager::CEntityManager()
{
    m_entities.clear();
}

CEntityManager::~CEntityManager()
{
    m_entities.clear();
}

void CEntityManager::createEntity(const std::string& id, CEntity::Entity_Type type)
{
    std::unique_ptr<CEntity> tempEntity(new CEntity(type));

    m_entities[id] = std::move(tempEntity);

    std::cout << "Successfully created entity called " << id << "!" << std::endl;
}

void CEntityManager::deleteEntity(const std::string& id)
{
    auto temp = m_entities.find(id);

    if (temp != m_entities.end())
    {
        m_entities.erase(temp);

        std::cout << "Successfully deleted entity with id " << id << "!" << std::endl;
    }

    else
        std::cout << "Error locating entity with id " << id << "." << std::endl;
}

std::unique_ptr<CEntity> CEntityManager::operator() (const std::string& id)
{
    auto temp = m_entities.find(id);

    if (temp != m_entities.end())
    {
        std::cout << "Successfully found entity with id " << id << "!" << std::endl;
        return temp->second;
    }

    else
    {
        std::cout << "Error locating entity with id " << id << "." << std::endl;
        return nullptr;
    }
}

My problem arises right at the return temp->second in the overloaded operator. Thanks to all who help.

Never mind, I figured out the problem. I forgot to std::move the temp->second.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.