首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的功能有什么问题?

我的功能有什么问题?
EN

Stack Overflow用户
提问于 2016-09-10 16:58:02
回答 2查看 41关注 0票数 0

我对c++相当陌生,并且正在处理各种函数。我似乎不明白为什么下面的代码不工作,任何帮助都会非常感激。

代码语言:javascript
复制
#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;
}

我得到的构建错误

代码语言:javascript
复制
`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)) ===|`
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-10 17:01:24

你提交了一份声明

代码语言:javascript
复制
int movieOutput(string movieName,int aTix = 0,int cTix = 0,
                float grPro = 0.0,float nePro = 0.0,float diPro = 0.0);

出现在main()前。

此外,默认参数需要放在函数声明中,而不是定义签名中。

这是固定代码:

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2016-09-10 17:03:31

只需在调用x之前声明函数即可)

代码语言:javascript
复制
int movieOutput(string, int, int, float, float, float); // function prototype 

int main()... 

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

或者简单地将整个函数声明放在主程序前面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39428468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档