我的函数出了什么问题?

What is wrong with my functions

本文关键字:问题 什么 函数 我的      更新时间:2023-10-16

我是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: tt" << movieName << endl;
    cout << "Adult Tickets Sold: tt" << aTix << endl;
    cout << "Child Tickets Sold: tt" << 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)) ===|`

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: tt" << movieName << endl;
    cout << "Adult Tickets Sold: tt" << aTix << endl;
    cout << "Child Tickets Sold: tt" << 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;
}

在调用之前声明你的函数x)

int movieOutput(string, int, int, float, float, float); // function prototype 
int main()... 
int movieOutput(...) { /* declaration goes here */} 

或者干脆把整个函数声明放在主函数

之前