2012-06-07 58 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; 
}