我对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)) ===|`
答案 0 :(得分: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;
}
答案 1 :(得分:0)
在调用之前声明你的函数x)
int movieOutput(string, int, int, float, float, float); // function prototype
int main()...
int movieOutput(...) { /* declaration goes here */}
或者只是简单地将整个函数声明放在主
之前