Visual Studio C++ & Boost Lib:仅当存在BOOST_FOREACH时才无法找到函数定义
Visual Studio C++ & Boost Lib: cannot find function definition only when BOOST_FOREACH is present
我最近已经安装了Visual Studio,并开始使用SFML库和Boost库工作,但是当我声明静态时,我偶然发现了这个奇怪的(对我来说)错误Visual Studio在静态类的标头文件中的空隙函数告诉我,当且仅当功能中存在BOOST_FOREACH代码时,找不到'FindTextures'的函数定义。你们中有人知道为什么会发生这种情况吗?谢谢一堆。
这将是textureloader.h:
#include <SFML/Graphics.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <iostream>
class TextureLoader {
public:
static const sf::Texture& getTexture(sf::String l_name);
static void findTextures();
private:
static std::map<sf::String, sf::Texture> textures;
};
这是textureloader.cpp类:
#include "TextureLoader.h"
// Get texture using name
const sf::Texture& TextureLoader::getTexture(sf::String l_name) {
return textures.at(l_name);
}
void TextureLoader::findTextures() {
namespace fs = boost::filesystem;
fs::path targetDir("/Textures");
fs::directory_iterator it(targetDir), eod;
BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod)) {
if(fs::is_regular_file(p)) {
std::cout << p.filename();
}
}
}
输出:
1>------ Build started: Project: MasterTest, Configuration: Debug Win32 ------
1>TextureLoader.cpp
1>Unknown compiler version - please run the configure tests and report the results
1>TextureLoader.obj : error LNK2001: unresolved external symbol "private: static class std::map<class sf::String,class sf::Texture,struct std::less<class sf::String>,class std::allocator<struct std::pair<class sf::String const ,class sf::Texture> > > TextureLoader::textures" (?textures@TextureLoader@@0V?$map@VString@sf@@VTexture@2@U?$less@VString@sf@@@std@@V?$allocator@U?$pair@$$CBVString@sf@@VTexture@2@@std@@@5@@std@@A)
1>F:CodingVSProjectsMasterTestDebugMasterTest.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "MasterTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
错误日志
好吧,请仔细阅读丢失符号的名称。它说:
std::map</*…*/> TextureLoader::textures
这不是任何功能。它是静态成员变量。而且,好吧,您确实缺少了这一点。您的 .cpp
文件应包括其定义,看起来像:
std::map<sf::String, sf::Texture> TextureLoader::textures;
因为声明的静态成员变量不够。您也必须定义它们。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 自动定义来自CRTP的访问者(CRTP使用boost foreach和boost变体)
- c++11 foreach语法和自定义迭代器
- 为什么 gcc 使用我的自定义迭代器优化这个 C++11 foreach 循环