2016-09-10 2 views
0

Ich bin ziemlich neu in C++ und bin mit Funktionen rum. Ich kann nicht herausfinden, warum der folgende Code nicht funktioniert, jede Hilfe würde sehr geschätzt werden.Was ist los mit meinen Funktionen

#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; 
} 

Die Build-Fehler Ich erhalte

`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)) ===|` 
+2

Bewegen Sie die Definition des Aufrufs main unter der Funktion –

+0

als @JosephYoung schlägt vor oder schreibe eine forward Deklarationszeile für diese Funktion "über" die erste Funktion "Aufruf" es (hier Haupt) – Dilettant

+0

Danke Leute, ihr habt mir geholfen es herauszufinden! – DaltonM

Antwort

0

Sie stellen eine Vorwärts-Erklärung

int movieOutput(string movieName,int aTix = 0,int cTix = 0, 
       float grPro = 0.0,float nePro = 0.0,float diPro = 0.0); 

vor main() erscheinen.

Auch Standardparameter müssen in der Funktionsdeklaration nicht in die Definitionssignatur gehen.

Hier ist der feste Code:

#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

einfach Ihre Funktion deklarieren, bevor es x)

int movieOutput(string, int, int, float, float, float); // function prototype 

int main()... 

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

Oder nur einfach gesagt die ganze Funktionsdeklaration vor Ihrer Haupt

+0

Es gibt immer noch ein Problem mit den Standardparameterwerten. –