esms.cpp:234: 错误:“the_config”未在此范围内声明
我正在尝试从不再有支持社区的旧开源项目编译一些 .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++
函数
the_config
并未在config.h
中真正声明。如果没有声明,esms.cpp
不知道the_config
是什么,因此编译器会生成错误。您确实在
config
类定义中具有the_config
的友元声明,但该友元声明不会生成的“正常”声明the_config
。上面的the_config
友元声明只是引用封闭范围(在本例中为全局范围)中的函数,但它没有在全局范围中引入声明。您需要明确地进行该声明,即您必须
在
config.h
中的类定义之外添加。您不能。 C++ 编译器(狭义上的术语,即“编译器”而不是“链接器”)独立地查看每个翻译单元,这意味着
esms.cpp
不知道也不关心config.cpp
。让esms.cpp
了解the_config
的唯一方法是在esms.cpp
中引入the_config
的声明>。这通常是通过使用头文件来完成的。在你的情况下,这将是config.h
。您需要修复您的config.h
,如上所示。Function
the_config
is not really declared inconfig.h
. Without a declarationesms.cpp
does not know whatthe_config
is, so the compiler generates the error.You do have a friend declaration of
the_config
insideconfig
class definition, but that friend declaration does not produce a "normal" declaration ofthe_config
. The above friend declaration ofthe_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
outside of the class definition in
config.h
.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 aboutconfig.cpp
. The only way to makeesms.cpp
to know aboutthe_config
is to introduce a declaration ofthe_config
intoesms.cpp
. That is normally done by using a header file. In your case that would beconfig.h
. You need to fix yourconfig.h
as shown above.