Greg Greg - 1 month ago 7
C++ Question

C++ error "invalid constructor; your probably meant 'Account (const Account&)

I keep getting the error "invalid constructor; your probably meant 'Account (const Account&). It's for line 10 of the code ...

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


I've tried changing the input to be (Account balance) and tried also using (int balance). int balance gives me an error of "expected primary-expression before ':' token.

Full code is...

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

cout << endl;


myAccount1.displayBalance();
}

H.G H.G
Answer

you need to fix your constructor and the initialization list, as follows

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

you need to provide an argument when creating Account object, as follows

int startBalance = 0;
Account myAccount1(startBalance);