2016-11-02 10 views
0

Ich erhalte den FehlerC++ Fehler „ungültig Konstruktor, Sie wahrscheinlich gemeint‚Account (konst Account &)

ungültig Konstruktor;. Sie wahrscheinlich Account (const Account&) gemeint

explicit Account (Account balance) { 
    : accountBalance (balance); 
} 

Ich habe versucht, Ändern der Eingabe zu (Account balance) und versuchte auch mit (int balance). int Gleichgewicht gibt mir einen Fehler

"erwarteter Primärausdruck vor ':' Token.

Voll Code

#include <iostream> 
#include <string> 
using namespace std; 

class Account { 
public: 

    explicit Account (Account balance) { 
     : accountBalance (balance); 
    } 

    void setAccountBalance (int balance) { 
     accountBalance = balance; 
    } 

    int getAccountBalance() const { 
     return accountBalance; 
    } 

    void displayBalance() const { 
     std::cout << "Welcome to the Account. The balance is : $" << getAccountBalance() << std::endl; 
    } 

private: 
    int accountBalance; 
}; 

int main() { 
    int startBalance = 0; 
    Account myAccount1; 

    cout << "Initial Dollar Amount is : " << myAccount1.getAccountBalance() << endl; 
    cout << "Please enter new balance : " << endl; 
    cin >> startBalance; 

    myAccount1.setAccountBalance(startBalance); 
    myAccount1.displayBalance(); 
} 

Antwort

2

Sie Ihren Konstruktor und die Initialisierung Liste reparieren müssen, wie

folgt
explicit Account (int balance):accountBalance (balance){} 

benötigen Sie ein Argument liefern, wenn Account Objekt zu erzeugen, wie

folgen
int startBalance = 0; 
Account myAccount1(startBalance); 
+0

Ahh ja danke. Ich sehe jetzt, dass ich setzen: accountBalance (balance); Innerhalb der Funktion des expliziten Accounts (int balance). Und ich legte auch ein Semikolon am Ende. ^.^Danke für deine Hilfe. – Greg

+0

@Greg Sie willkommen! .. Sie könnten eine der Antworten auf Ihre Frage akzeptieren .. für weitere Informationen http://StackOverflow.com/Help/accepted-answer – HazemGomaa

3

ersetzen

Account (Account balance) 

mit

Account (int balance) 
//  ^^^ 
+0

int Balance gibt mir einen Fehler von „erwarteten Primärausdruck vor‚:‘Token. – Greg