开发者

esms.cpp:234: error: 'the_config' was not declared in this scope

开发者 https://www.devze.com 2023-03-03 20:48 出处:网络
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 ca

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++


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消