2

I'm an amateur C++ programmer trying to make simple game using SFML. I am using a resource manager but was recently advised to avoid using pointers if possible. I wanted to try replacing them with smart pointers, but I haven't quite gotten the hang of it just yet.

When running this following code:

ResourceManager.h

    #ifndef _RESOURCE_MANAGER_
#define _RESOURCE_MANAGER_

#include "tinyxml2.h"

#include <SFML\Graphics\Texture.hpp>
#include <SFML\Graphics\Font.hpp>
#include <SFML\Audio\SoundBuffer.hpp>
#include <SFML\Audio\Music.hpp>

#include <map>
#include <string>
#include <memory>

class ResourceManager
{
public:
    static void destroy();
    const static std::shared_ptr <sf::Texture> getTexture(std::string fileName);
    const static std::shared_ptr <sf::SoundBuffer> getSoundBuffer(std::string fileName);
    const static std::shared_ptr <sf::Music> getMusic(std::string fileName);
    const static std::shared_ptr <sf::Font> getFont(std::string fileName);
    const static tinyxml2::XMLDocument& getLevelXML(std::string fileName);
    const static tinyxml2::XMLDocument& getProfileXML(std::string fileName);
    const static tinyxml2::XMLDocument& getMenuXML(std::string fileName);
    const static tinyxml2::XMLDocument& getConfigXML(std::string fileName);

private:
    ResourceManager();
    const static tinyxml2::XMLDocument& getXML(std::string path, std::string fileName);
    static std::map <std::string, std::shared_ptr <sf::Texture>> mTextures;
    static std::map <std::string, std::shared_ptr <sf::SoundBuffer>> mSoundBuffers;
    static std::map <std::string, std::shared_ptr <sf::Music>> mMusics;
    static std::map <std::string, std::shared_ptr <sf::Font>> mFonts;
    static std::map <std::string, std::shared_ptr <tinyxml2::XMLDocument>> mXML;
    static const std::string GRAPHICS_PATH, SOUND_PATH, MUSIC_PATH, FONT_PATH, LEVEL_XML_PATH, PROFILE_XML_PATH, MENU_XML_PATH, CONFIG_XML_PATH;
};

#endif // _RESOURCE_MANAGER_

ResourceManager.cpp

#include "ResourceManager.h"
#include <cassert>
#include "DebugOut.h"


const std::string 
    ResourceManager::GRAPHICS_PATH      = "./resources/graphics/",
    ResourceManager::SOUND_PATH         = "./resources/sounds/",
    ResourceManager::MUSIC_PATH         = "./resources/music/",
    ResourceManager::FONT_PATH          = "./resources/fonts/",
    ResourceManager::LEVEL_XML_PATH     = "./resources/xml/levels/",
    ResourceManager::PROFILE_XML_PATH   = "./resources/xml/profiles/",
    ResourceManager::MENU_XML_PATH      = "./resources/xml/menus/",
    ResourceManager::CONFIG_XML_PATH    = "./resources/xml/config/";


std::map <std::string, std::shared_ptr <sf::Texture>> mTextures;
std::map <std::string, std::shared_ptr <sf::SoundBuffer>> mSoundBuffers;
std::map <std::string, std::shared_ptr <sf::Music>> mMusics;
std::map <std::string, std::shared_ptr <tinyxml2::XMLDocument>> mXML;
std::map <std::string, std::shared_ptr <sf::Font>> mFonts;


ResourceManager::ResourceManager()
{
}

void ResourceManager::destroy()
{
    // Destroy and deallocate all loaded resources
    /*
    for(auto i = mTextures.begin(); i != mTextures.end(); )
    {
        delete i->second;
        i = mTextures.erase(i);
    }

    for(auto i = mSoundBuffers.begin(); i != mSoundBuffers.end(); )
    {
        delete i->second;
        i = mSoundBuffers.erase(i);
    }

    for(auto i = mMusics.begin(); i != mMusics.end(); )
    {
        delete i->second;
        i = mMusics.erase(i);
    }

    for(auto i = mXML.begin(); i != mXML.end(); )
    {
        delete i->second;
        i = mXML.erase(i);
    }

    for(auto i = mFonts.begin(); i != mFonts.end(); )
    {
        delete i->second;
        i = mFonts.erase(i);
    }*/
}

// Fetch a sprite by getting the respective image filename
const std::shared_ptr <sf::Texture> ResourceManager::getTexture(std::string fileName)
{
    if(mTextures.find(fileName) == mTextures.end())
    {
        auto newTexture = std::shared_ptr<sf::Texture>(new sf::Texture());
        if(fileName == "")
        {
            newTexture->create(0,0);            
            dbgo::println("ResourceManager.cpp: createEmptyTexture");
        }
        else
        {
            newTexture->loadFromFile(GRAPHICS_PATH + fileName);
        }
        mTextures[fileName] = std::move(newTexture);
    }

    return mTextures[fileName];
}

// Fetch a sound by calling the respective sound filename
const std::shared_ptr <sf::SoundBuffer> ResourceManager::getSoundBuffer(std::string fileName)
{
    if(mSoundBuffers.find(fileName) == mSoundBuffers.end())
    {
        auto newSoundBuffer = std::shared_ptr<sf::SoundBuffer>(new sf::SoundBuffer());
        if(fileName != "")
        {
            newSoundBuffer->loadFromFile(SOUND_PATH + fileName);
        }
        else
        {
            dbgo::println("ResourceManager.cpp: Empty fileName! SoundBuffer");
        }
        mSoundBuffers[fileName] = std::move(newSoundBuffer);
    }

    return mSoundBuffers[fileName];
}

// May require update/fix
// Fetch music by calling the respective music filename
const std::shared_ptr <sf::Music> ResourceManager::getMusic(std::string fileName)
{
    if(mMusics.find(fileName) == mMusics.end())
    {
        auto newMusic = std::shared_ptr<sf::Music>(new sf::Music());
        bool success = newMusic->openFromFile(MUSIC_PATH + fileName);
        assert(success);
        mMusics[fileName] = std::move(newMusic);
    }

    return mMusics[fileName];
}

const std::shared_ptr <sf::Font> ResourceManager::getFont(std::string fileName)
{
    if(mFonts.find(fileName) == mFonts.end())
    {
        auto newFont = std::shared_ptr<sf::Font>(new sf::Font());
        newFont->loadFromFile(FONT_PATH + fileName);
        mFonts[fileName] = std::move(newFont);
    }

    return mFonts[fileName];
}

const tinyxml2::XMLDocument& ResourceManager::getLevelXML(std::string fileName)
{
    return getXML(LEVEL_XML_PATH, fileName);
}

const tinyxml2::XMLDocument& ResourceManager::getProfileXML(std::string fileName)
{
    // TODO: ensure that we do not deliver a stale file!
    return getXML(PROFILE_XML_PATH, fileName);
}

const tinyxml2::XMLDocument& ResourceManager::getMenuXML(std::string fileName)
{
    return getXML(MENU_XML_PATH, fileName);
}

const tinyxml2::XMLDocument& ResourceManager::getConfigXML(std::string fileName)
{
    return getXML(CONFIG_XML_PATH, fileName);
}

const tinyxml2::XMLDocument& ResourceManager::getXML(std::string path, std::string fileName)
{
    if(mXML.find(fileName) == mXML.end())
    {
        auto newXML = std::shared_ptr<tinyxml2::XMLDocument>(new tinyxml2::XMLDocument());
        const std::string filepath = path + fileName + ".xml";
        tinyxml2::XMLError retVal = newXML->LoadFile(filepath.c_str());
        if(retVal != tinyxml2::XML_NO_ERROR)
        {
            dbgo::println("ResourceManager: Error loading XML document " + filepath);
            assert(false);
            std::exit(retVal);
        }
        mXML[fileName] = std::move(newXML);
    }

    return *mXML[fileName];
}

I receive these errors in Visual Studio 2013:

1>------ Build started: Project: SimpleFrame, Configuration: Debug Win32 ------
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::shared_ptr<class sf::Texture>,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::shared_ptr<class sf::Texture> > > > ResourceManager::mTextures" (?mTextures@ResourceManager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTexture@sf@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTexture@sf@@@2@@std@@@2@@std@@A)
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::shared_ptr<class sf::SoundBuffer>,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::shared_ptr<class sf::SoundBuffer> > > > ResourceManager::mSoundBuffers" (?mSoundBuffers@ResourceManager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VSoundBuffer@sf@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VSoundBuffer@sf@@@2@@std@@@2@@std@@A)
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::shared_ptr<class sf::Music>,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::shared_ptr<class sf::Music> > > > ResourceManager::mMusics" (?mMusics@ResourceManager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VMusic@sf@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VMusic@sf@@@2@@std@@@2@@std@@A)
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::shared_ptr<class sf::Font>,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::shared_ptr<class sf::Font> > > > ResourceManager::mFonts" (?mFonts@ResourceManager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VFont@sf@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VFont@sf@@@2@@std@@@2@@std@@A)
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::shared_ptr<class tinyxml2::XMLDocument>,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::shared_ptr<class tinyxml2::XMLDocument> > > > ResourceManager::mXML" (?mXML@ResourceManager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VXMLDocument@tinyxml2@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VXMLDocument@tinyxml2@@@2@@std@@@2@@std@@A)
1>G:\Library - Documents\GitHub\SimpleFrame\Debug\SimpleFrame.exe : fatal error LNK1120: 5 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Does anyone have an idea what I am doing wrong? I expect to the error to be caused by my use of shared_ptr in the functions.

I appreciate all the help and input I receive!

  • 1
    This is not a duplicate of that at all. In this case, five static `map`s are being declared in the class header, but never defined (as indicated by the linker). You're missing the `ResourceManager::` prefix before the name of each definition in the .cpp file -- otherwise you're defining 5 maps *outside* of the class that are completely unrelated. – Cameron Sep 16 '14 at 20:23
  • 1
    The member variables are declared as part of the class but then defined in the global scope in the cpp file. Adding `ResourceManager::` to them should do the trick. – Niall Sep 16 '14 at 20:23
  • Related answer in another post http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574403#12574403 – Niall Sep 16 '14 at 20:28
  • This was indeed the problem, thank you all very much! – Alexander Milton Sep 16 '14 at 21:19

1 Answers1

4

You forgot to add the class name to these:

std::map <std::string, std::shared_ptr <sf::Texture>> mTextures;
std::map <std::string, std::shared_ptr <sf::SoundBuffer>> mSoundBuffers;
std::map <std::string, std::shared_ptr <sf::Music>> mMusics;
std::map <std::string, std::shared_ptr <tinyxml2::XMLDocument>> mXML;
std::map <std::string, std::shared_ptr <sf::Font>> mFonts;

Change them to

std::map <std::string, std::shared_ptr <sf::Texture>> ResourceManager::mTextures;
std::map <std::string, std::shared_ptr <sf::SoundBuffer>> ResourceManager::mSoundBuffers;
std::map <std::string, std::shared_ptr <sf::Music>> ResourceManager::mMusics;
std::map <std::string, std::shared_ptr <tinyxml2::XMLDocument>> ResourceManager::mXML;
std::map <std::string, std::shared_ptr <sf::Font>> ResourceManager::mFonts;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Oh my, I would have never figured that out with my head spinning around the pointers. Your suggestion did the trick excellently. Thank you very, very much! – Alexander Milton Sep 16 '14 at 21:18