【问题标题】:Visual Studio C++ & Boost Lib: cannot find function definition only when BOOST_FOREACH is presentVisual Studio C++ 和 Boost Lib:仅当 BOOST_FOREACH 存在时找不到函数定义
【发布时间】:2017-12-11 11:15:05
【问题描述】:

我最近安装了 Visual Studio 并开始使用 SFML 库和 Boost 库,但是当我在头文件中声明一个静态 void 函数时,我偶然发现了这个奇怪的(对我而言)错误静态类,Visual Studio 告诉我“找不到‘findTextures’的函数定义”当且仅当函数中存在 BOOST_FOREACH 代码时。大家知道为什么会这样吗?谢谢一堆。

这将是 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:\Coding\VSProjects\MasterTest\Debug\MasterTest.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 ==========

The error log

【问题讨论】:

  • 如果没有 Visual Studio 的 exactcomplete 输出,我们将无能为力。引用错误消息(来自 output 窗口,不是错误消息!)对于任何人都能够帮助您至关重要。
  • @JanHudec 刚刚编辑了问题并添加了错误日志:)
  • 请作为 OUTPUT 窗口中的 TEXT
  • 请注意,错误窗口也允许文本复制和粘贴,但我从一开始就说过您应该从输出窗口中获取它,而不是错误窗口。
  • “未知编译器版本 - 请运行配置测试并报告结果”。我怀疑你使用的是 VS2017 版本 15.5,目前 boost 不支持。

标签: c++ visual-studio boost


【解决方案1】:

好吧,仔细阅读缺失符号的名称。它说:

std::map</*…*/> TextureLoader::textures

这不是任何功能。它是静态成员变量。而且,你确实错过了这一点。您的 .cpp 文件应包含其定义,如下所示:

std::map<sf::String, sf::Texture> TextureLoader::textures;

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

【讨论】:

  • 注意:在这里您还可以看到为什么确切的输出如此重要。问题原文只是说“Function definition for 'findTextures'”,结果发现是一个完全不同的符号就是问题所在。
  • 好吧,尽管“'findTextures' 的函数定义未找到”消息仍然显示为绿色下划线,但它似乎完成了这项工作。谢谢!
  • @Crack498,编辑器内的错误高亮非常有用,但不能始终信任它。这就是为什么检查实际编译器输出很重要的原因。错误窗口通常就足够了,但有时还有仅在输出窗口中可用的附加信息(尤其是在涉及模板时)。
猜你喜欢
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2017-02-24
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多