C++如何从主系统移动代码

C++ how can I move a code from the main

本文关键字:系统 移动 代码 C++      更新时间:2023-10-16

我对C++知之甚少所以我有这个代码

bool is_successful = true;
ex_file_licensing exFileLicence;
std::string flexLMfilePath;
flexLMfilePath.append("C:/Desktop/QA-program/testsuite/tmp/");
std::string Message = exFileLicence.checkLicense(DI_MF,flexfilePath,is_successful);

我被要求将其移到主线之外,然后在主线中调用它现在我不知道该怎么办你能告诉我我应该遵循的步骤是什么吗请尽可能具体,我真的很不擅长这件事

谢谢

您必须创建一个函数并在 main 中调用该函数:

void foo(); //this is called a function prototype
main()
{
...
foo() //your function in place of that code
}
void foo()
{
...//the code originally in main.  This is called your function definition
}

这就是创建函数的工作方式,基本上是 C++ 中任何代码的编写方式。 有时函数出现在主文件之外的文件中,但基本相同。

查看C++函数。我假设你有如下内容。

int main(){
    //***your stuff
return

您需要以下内容。

void function(){
    //**your stuff
return;
}
int main(){
      function();
return;
}
当程序启动时,它

将自动转到主,当它到达呼叫时: 函数();

它将控制权传递给包装在其中的代码

void function(){
return;
}

如果我理解正确,我认为您只需要将代码放入函数中,如下所示:

void CodeFunction()
{
    bool is_successful = true; 
    ex_file_licensing exFileLicence; 
    std::string flexLMfilePath; 
    flexLMfilePath.append("C:/Desktop/QA-program/testsuite/tmp/"); 
    std::string Message = exFileLicence.checkLicense(DI_MF,flexfilePath,is_successful); 
}

然后,您可以使用 CodeFunction()main调用它。

请记住将其放在main函数上方,或者如果它低于此值,则在上面声明它main使用

void CodeFunction();

希望这有帮助。

您需要编写一个函数,将代码移动到该函数,然后从 main - http://www.cplusplus.com/doc/tutorial/functions/调用该函数