gpt4 book ai didi

c++ - 使用 shared_ptr 时未解析的外部符号

转载 作者:行者123 更新时间:2023-11-30 02:43:46 24 4
gpt4 key购买 nike

我是一名业余 C++ 程序员,尝试使用 SFML 制作简单的游戏。我正在使用资源管理器,但最近有人建议我尽可能避免使用指针。我想尝试用智能指针替换它们,但我还没有完全掌握它。

运行以下代码时:

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];
}

我在 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 ==========

有人知道我做错了什么吗?我希望错误是由于我在函数中使用 shared_ptr 引起的。

我感谢收到的所有帮助和意见!

最佳答案

你忘了给这些添加类名:

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;

将它们更改为

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;

关于c++ - 使用 shared_ptr 时未解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com