- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名业余 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/
使用 shared_ptr 时,我应该只使用 shared_ptr 吗?申报一次或申报shared_ptr无论我经过哪里? 所以在我新建实例的函数中,我将它包装在 shared_ptr 中但是当我从函
#include #include #include using namespace std; struct Node { Node(int data, boost::shared_pt
对于我目前正在处理的代码,我们有时需要使用较旧的编译器在一些较旧的系统上进行编译(例如,我们在较旧的 IBM BlueGene/L 上运行 sims,其支持契约(Contract)规定了一些非常旧的
我正在阅读 this answer作者指的是boost best practices其中说: Avoid using unnamed shared_ptr temporaries to save ty
我正在处理几个类,我想知道如何在我的应用程序类中使用普通成员,而该成员需要使用 shared_from_this()? 这里有一些代码来阐明我的意思(见评论) class Observable { p
我有一个 Foo 类,其中包含一个 Hotel 类 的 shared_ptr,以及一个 Rules 类(位于 namespace Rules 内): class Foo { public: //
我想不通。看起来我遗漏了一些简单的东西?我要在 MakePointToSameValue 中输入什么,以便在点 (1) b.ptr 和c.ptr 与a.ptr 指向同一个 换句话说,a.ptr.get
我已尽我所能制作了 SSCE。我怀疑共享指针在我在 main 中请求它们之前解构(释放)了我的对象。如何在不完全绕过共享指针的情况下防止这种情况发生?这是一个程序中的孤立问题,否则可以通过使用 sha
这个问题在这里已经有了答案: Set shared_ptr with new_pointer that is old_pointer + offset (1 个回答) 关闭 4 年前。 我目前正在学
假设我们有一个类,成员如下 std::map> member_我们无法替换 member通过具有 std::shared_ptr 的 map ,因为该类必须对 ObscureType 的非常量函数进行
我正在用 C++ 做学校作业(我还在学习)。我正在尝试实现随机生成的二叉树结构,使用 shared_ptr 在多个地方存储节点的信息(我需要它作为作业)。考虑以下示例代码(这是我的小测试程序): #i
我有以下类(class) struct Images { std::vector > ptr_vector; } 将 ptr_vector 放入 std::shared_ptr 中不会在复制
我刚刚对一个项目进行了大规模重构,添加了一个基类来代替现在所说基类的派生类(因为我想要这个类的更多“类型”)。 我的问题是,一些实用函数将原始类 A 的引用作为 shared_ptr,因此函数声明如下
我记得 Scott Meyers 教我的 func(shared_ptr(new P), shared_ptr(new Q)); 是危险的,因为(如果我没记错的话)内存分配、引用计数(构造)和分配给
给定 struct X { void f(std::shared_ptr); }; auto x(std::make_shared()); 我大概可以安全地做 x->f(std::move(x
我试着介绍了一些const一些新代码的正确性(实际上是功能范例),发现我无法传递 std::shared_ptr到一个需要 std::shared_ptr 的函数.请注意,我不想放弃 constnes
我需要将原始指针包装到 shared_ptr 中,以便将其传递给函数。该函数在返回后不保留对输入对象的任何引用。 { MyClass i; shared_ptr p(&i); f(p);
我在继承链中有4个类:A-> B-> C,A-> B-> D,其中B是唯一的类模板。 我想拥有一个在id和对象指针(C或D)之间映射的std::map,但是我在将make_shared输出分配给std
boost::shared_ptr 是否解决原始指针问题? Base* p = new Base(); shared_ptr sp(p); shared_ptr sq(p); 两个 shared_
How can shared_ptr be a subclass of shared_ptr? 我想知道如何实现模板类 C这样 C是 C 的子类? 我已经观察到上述情况,例如 shared_ptr和
我是一名优秀的程序员,十分优秀!