[英]How to get global variable declared in Main.cpp to be accessible in a header
How would I get a global variable to appear in a header that's included after the variable's declaration? 如何将全局变量显示在变量声明后包含的标题中? I have it arranged like this, but within the header, it's not recognized as being defined:
我有这样安排,但在标题内,它不被识别为被定义:
main.cpp: main.cpp中:
const int COUNT = 10;
void (* FUNCTION[COUNT])();
const char * DESCRIPTION[COUNT];
#include "TestFont.h"
testFont.h: testFont.h:
#define COUNTER_VAR = __COUNTER__;
DESCRIPTION[COUNTER_VAR] = "description";
FUNCTION[COUNTER_VAR] = func;
#undef COUNTER_VAR
In the header, DESCRIPTION
and FUNCTION
are seen as undefined, how would I make it so they can be used? 在标题中,
DESCRIPTION
和FUNCTION
被视为未定义,我将如何制作它们以便可以使用它们?
Disclaimer: It's super ugly isn't it! 免责声明:这是超级难看的不是它! This is strictly for a sandbox project in VS, so I can write small single-file tests or scripts in C++ with as little IDE setup ceremony as possible.
这严格用于VS中的沙箱项目,因此我可以使用尽可能少的IDE设置仪式在C ++中编写小型单文件测试或脚本。 The goal is to make it so all I need do is include the header in main.cpp, and main will have the necessary function pointer and description to list it as an option in the console.
目标是使它成为我需要做的就是在main.cpp中包含标题,main将具有必要的函数指针和描述,以将其列为控制台中的选项。 No grades please!
没有成绩请!
The following lines are not legal outside of a function body. 以下行在函数体之外是不合法的。
DESCRIPTION[COUNTER_VAR] = "description";
FUNCTION[COUNTER_VAR] = func;
You may need to wrap that in a helper function. 您可能需要将其包装在辅助函数中。
static int init()
{
#define COUNTER_VAR = __COUNTER__;
DESCRIPTION[COUNTER_VAR] = "description";
FUNCTION[COUNTER_VAR] = func;
#undef COUNTER_VAR
}
and call the function. 并调用该函数。
static int dummy = init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.