2016-09-10 240 views
0

我對C++比較陌生,並且正忙於功能。 我似乎無法弄清楚爲什麼下面的代碼不工作, 任何幫助將不勝感激。我的功能出了什麼問題

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

int main() { 
    movieOutput("Hello"); 

    return 0; 
} 

//This is just for a little extra versatility 
int movieOutput(string movieName,int aTix = 0,int cTix = 0,float grPro = 0.0,float nePro = 0.0,float diPro = 0.0){ 

    //I don't understand whether I should declare the arguments inside the 
    //functions parameters or in the function body below. 

    /*string movieName; 
     int aTix = 0, cTix = 0; 
     float grPro = 0.0, nePro = 0.0, diPro = 0.0;*/ 

    cout << "**********************Ticket Sales********************\n"; 
    cout << "Movie Name: \t\t" << movieName << endl; 
    cout << "Adult Tickets Sold: \t\t" << aTix << endl; 
    cout << "Child Tickets Sold: \t\t" << aTix << endl; 
    cout << "Gross Box Office Profit: \t" << grPro << endl; 
    cout << "Net Box Office Profit: \t" << nePro << endl; 
    cout << "Amount Paid to the Distributor: \t" << diPro << endl; 

    return 0; 
} 

構建的錯誤我得到

`Build:(compiler: GNU GCC Compiler) 
|line-8|error: 'movieOutput' was not declared in this scope| 
Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|` 
+2

移動的定義主要在使用的函數 –

+0

之下,因爲@JosephYoung建議或爲該函數寫一個前向聲明行,「在」第一個函數「之上」調用它(這裏是main) – Dilettant

+0

謝謝你們,你們都幫我弄明白了! – DaltonM

回答

0

你把

int movieOutput(string movieName,int aTix = 0,int cTix = 0, 
       float grPro = 0.0,float nePro = 0.0,float diPro = 0.0); 

main()出庭前向聲明。

此外,缺省參數需要進入函數聲明而不是定義簽名。

這裏的固定碼:

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

int movieOutput(string movieName,int aTix = 0,int cTix = 0, 
       float grPro = 0.0,float nePro = 0.0,float diPro = 0.0); 

int main() { 
    movieOutput("Hello"); 

    return 0; 
} 

//This is just for a little extra versatility 
int movieOutput(string movieName,int aTix,int cTix,float grPro,float nePro,float diPro){ 

    cout << "**********************Ticket Sales********************\n"; 
    cout << "Movie Name: \t\t" << movieName << endl; 
    cout << "Adult Tickets Sold: \t\t" << aTix << endl; 
    cout << "Child Tickets Sold: \t\t" << aTix << endl; 
    cout << "Gross Box Office Profit: \t" << grPro << endl; 
    cout << "Net Box Office Profit: \t" << nePro << endl; 
    cout << "Amount Paid to the Distributor: \t" << diPro << endl; 

    return 0; 
} 
0

調用前就宣佈你的函數x)

int movieOutput(string, int, int, float, float, float); // function prototype 

int main()... 

int movieOutput(...) { /* declaration goes here */} 

或之前只是簡單地把整個函數聲明你的主

+0

默認參數值仍存在問題。 –