简体   繁体   English

C ++预处理器#include

[英]C++ pre-processor #include

I'm trying to make my program into multiple languages, at start the user is asked if he wants language1 or language2. 我正在尝试将我的程序制作成多种语言,一开始,系统询问用户是否要使用language1或language2。 User input is stored in a variable and then using the if statement I get what language the user chose. 用户输入存储在变量中,然后使用if语句获得用户选择的语言。

Like this : 像这样 :

std::cin >> language;

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

What I did next is is I stored every function that I want to be translated into two header files, one English and one Spanish, both header files are the exact same, except every output is translated. 接下来要做的是将要翻译的每个函数存储为两个头文件,一个英语和一个西班牙语,两个头文件完全相同,只是每个输出都被翻译了。

Now what i did is something like this 现在我所做的就是这样

std::cin >> language;

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

Now, #include is a pre-processor directive so it gets "executed" before the main function, any way around this ? 现在, #include是一个预处理器指令,因此可以在主函数之前“执行”该指令吗?

Now, #include is postprocessor directive so it gets "executed" before the main function 现在,#include是后处理器指令,因此它会在主函数之前“执行”

It is a pre processor directive. 这是预处理程序指令。 It is not "executed" at runtime; 它不是在运行时“执行”的。 The source is pre-processed before compilation. 在编译之前对源进行了预处理。

any way around this ? 有什么办法解决吗?

There is no way to run pre-processor after the program has been compiled and executed. 编译并执行程序后,无法运行预处理器。

A better approach is to not duplicate the function definitions, but instead call a function to translate messages before printing output. 更好的方法是不复制函数定义,而是在打印输出之前调用函数以转换消息。 This translation function should map an argument string into a translated one. 此翻译功能应将参数字符串映射为已翻译的参数字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM