我的函数出了什么问题?
What is wrong with my functions
我是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 */}
或者干脆把整个函数声明放在主函数
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?