2012-06-07 56 views
-6

移动代码,我很少有knowlage在C++ 所以我有这样的代码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); 

,我被要求把它移到外面的主,然后调用它的主要 现在我不知道该怎么做 你能告诉我什么是我应该遵循 请尽可能具体的步骤,我真的不擅长这个东西

感谢

+7

你需要写一个函数包装的代码。但是,如果你真的没有使用C++的经验,你应该得到一个[体面的入门书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –

+0

有关如何创建功能的更多信息,请参阅http://www.cplusplus.com/forum/beginner/35690/ –

回答

0

如果我理解正确的话,我认为你只需要将代码放在一个函数,就像这样:

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功能,或者如果它是使用以下

void CodeFunction();

希望这有助于宣布它上面main

2

您必须创建一个函数,调用该函数内部主:

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++中的任何代码。有时函数会出现在主文件之外的文件中,但它的基本相同。

1

查看C++函数。我假设你有如下的东西。

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

您需要以下信息。

void function(){ 
    //**your stuff 
return; 
} 

int main(){ 

     function(); 

return; 
} 

程序启动时它会自动进入到主,当它到达的呼叫: 功能();

它将控制传递给内

void function(){ 

return; 
}