Visual Studio C++ & Boost Lib:仅当存在BOOST_FOREACH时才无法找到函数定义

Visual Studio C++ & Boost Lib: cannot find function definition only when BOOST_FOREACH is present

本文关键字:FOREACH 定义 函数 BOOST Boost C++ Studio Lib 存在 Visual      更新时间:2023-10-16

我最近已经安装了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;

因为声明的静态成员变量不够。您也必须定义它们。