我对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)) ===|`发布于 2016-09-10 17:01:24
你提交了一份声明
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;
}发布于 2016-09-10 17:03:31
只需在调用x之前声明函数即可)
int movieOutput(string, int, int, float, float, float); // function prototype
int main()...
int movieOutput(...) { /* declaration goes here */} 或者简单地将整个函数声明放在主程序前面。
https://stackoverflow.com/questions/39428468
复制相似问题