esms.cpp:234: 错误:“the_config”未在此范围内声明

发布于 11-05 12:44 字数 1661 浏览 4 评论 0原文

我正在尝试从不再有支持社区的旧开源项目编译一些 .cpp 文件。项目中大约有 15 个 .cpp 文件,其中几个文件使用一个名为 config.cpp 的通用文件。我使用 make 文件编译 C++ 源代码,该文件运行如下命令:

g++ -g -c -Wall -迂腐的 -ansi esms.cpp

但是,这给了我这样的几个错误: esms.cpp:234: 错误:'the_config' 未在此范围内声明

我检查了 esms.cpp 文件,它包含以下内容:

#include "game.h"
#include "config.h"
#include "tactics.h"
#include "report_event.h"
#include "teamsheet_reader.h"
#include "cond.h"
#include "util.h"
#include "mt.h"
#include "anyoption.h"
#include "cond_utils.h"
#include "config.h"
#include "comment.h"

我还检查了 config.h 文件并在文件中找到了以下内容:

class config
{
public:
  void load_config_file(string filename);
  string get_config_value(string key);
  void set_config_value(string key, string value);
  int get_int_config(string key, int dflt);

friend config& the_config(void);

private:
  config()
  {}
  config(const config& rhs);
  config& operator= (const config& rhs);

map<string, string> config_map;
};

然后我检查了config.cpp 文件并找到 the_config 的声明:

#include <cstdio>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cctype>
#include "config.h"
#include "util.h"


// get a reference to a static config (a singleton)

config& the_config(void)
{
    static config tcfg;
    return tcfg;

}

因此“the_config”似乎是在 config.h 和 config.cpp 文件中定义的,但由于某种原因 esms.cpp 文件没有定义认为这是宣布的。我尝试将该方法移至 esms.cpp 文件并将其从 config.cpp 文件中删除,但随后依赖于 config.cpp 文件的其他 cpp 文件开始出现错误。

如何使 esms.cpp 文件识别 config.cpp 中定义的“the_config”以修复范围错误?

感谢您的帮助!我希望这个问题有意义,我刚刚开始使用 C++

I'm trying to compile some .cpp files from an old open source project that no longer has a support community. There are about 15 .cpp files in the project and several of them use a one common file called config.cpp. I compile the c++ source code using a make file which runs a command like this:

g++ -g -c -Wall -pedantic -ansi
esms.cpp

However this gives me the several errors like this:
esms.cpp:234: error: 'the_config' was not declared in this scope

I checked the esms.cpp file and it has the following includes:

#include "game.h"
#include "config.h"
#include "tactics.h"
#include "report_event.h"
#include "teamsheet_reader.h"
#include "cond.h"
#include "util.h"
#include "mt.h"
#include "anyoption.h"
#include "cond_utils.h"
#include "config.h"
#include "comment.h"

I also checked the config.h file and found the following in the file:

class config
{
public:
  void load_config_file(string filename);
  string get_config_value(string key);
  void set_config_value(string key, string value);
  int get_int_config(string key, int dflt);

friend config& the_config(void);

private:
  config()
  {}
  config(const config& rhs);
  config& operator= (const config& rhs);

map<string, string> config_map;
};

Then I checked the config.cpp file and found the declaration for the_config:

#include <cstdio>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cctype>
#include "config.h"
#include "util.h"


// get a reference to a static config (a singleton)

config& the_config(void)
{
    static config tcfg;
    return tcfg;

}

So "the_config" appears to be defined in the config.h and config.cpp files, but for some reason the esms.cpp file doesn't think this is declared. I tried moving the method to the esms.cpp file and removing it from the config.cpp file but then other cpp files that depend on the config.cpp file started getting errors.

How do I make the esms.cpp file recognize that "the_config" is defined in the config.cpp to fix the scope error?

Thanks for your help! I hope this question makes sense, I'm just getting started with C++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪2024-11-12 12:44:05

函数the_config并未config.h中真正声明。如果没有声明,esms.cpp 不知道 the_config 是什么,因此编译器会生成错误。

确实config 类定义中具有 the_config 的友元声明,但该友元声明不会生成 的“正常”声明the_config。上面的 the_config 友元声明只是引用封闭范围(在本例中为全局范围)中的函数,但它没有在全局范围中引入声明。

您需要明确地进行该声明,即您必须

config& the_config(void);

config.h 中的类定义之外添加。


如何使 esms.cpp 文件识别出“the_config”是在 config.cpp 中定义的

您不能。 C++ 编译器(狭义上的术语,即“编译器”而不是“链接器”)独立地查看每个翻译单元,这意味着 esms.cpp 不知道也不关心 config.cpp。让 esms.cpp 了解 the_config 的唯一方法是在 esms.cpp 中引入 the_config 的声明>。这通常是通过使用头文件来完成的。在你的情况下,这将是config.h。您需要修复您的 config.h,如上所示。

Function the_config is not really declared in config.h. Without a declaration esms.cpp does not know what the_config is, so the compiler generates the error.

You do have a friend declaration of the_config inside config class definition, but that friend declaration does not produce a "normal" declaration of the_config. The above friend declaration of the_config simply refers to a function from the enclosing scope (global scope in this case), but it does not introduce a declaration in global scope.

You need to make that declaration explicitly, i.e. you have to add

config& the_config(void);

outside of the class definition in config.h.


How do I make the esms.cpp file recognize that "the_config" is defined in the config.cpp

You can't. C++ compiler (in narrow sense of the term, i.e. "compiler" as opposed to "linker") sees each translation unit independently, which means that esms.cpp does not know and does not care about config.cpp. The only way to make esms.cpp to know about the_config is to introduce a declaration of the_config into esms.cpp. That is normally done by using a header file. In your case that would be config.h. You need to fix your config.h as shown above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文