C++预处理器#include

C++ pre-processor #include

我正在尝试将我的程序制作成多种语言,在开始时会询问用户是想要语言 1 还是语言 2。用户输入存储在一个变量中,然后使用 if 语句我得到用户选择的语言。

像这样:

std::cin >> language;

if(language == ENGLISH)
     {
     // Do something
     }
else if(language == SPANISH)
     {
     // Do something else
     }

我接下来做的是将我想要翻译的每个函数存储到两个头文件中,一个是英语,一个是西班牙语,两个头文件完全相同,除了每个输出都被翻译。

现在我做的是这样的

std::cin >> language;

if(language == ENGLISH)
     {
     #include "English.h"
     }
else if(language == SPANISH)
     {
     #include "Spanish.h"
     }

现在,#include 是一个预处理器指令,所以它在主函数之前得到 "executed",有什么办法解决这个问题吗?

Now, #include is postprocessor directive so it gets "executed" before the main function

它是一个 pre 处理器指令。 运行 时不是 "executed";编译前对源码进行预处理

any way around this ?

程序编译执行后无法运行预处理器

更好的方法是不重复函数定义,而是在打印输出之前调用一个函数来翻译消息。此翻译函数应将参数字符串映射到翻译后的字符串。