gpt4 book ai didi

C++ 错误 "invalid constructor; your probably meant ' 帐户(常量帐户&)

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:22 25 4
gpt4 key购买 nike

一直报错

invalid constructor; your probably meant Account (const Account&).

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

我尝试将输入更改为 (Account balance) 并尝试使用 (int balance)。 int balance 给我一个错误

"expected primary-expression before ':' token.

完整代码

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

最佳答案

替换

Account (Account balance) 

Account (int balance) 
// ^^^

关于C++ 错误 "invalid constructor; your probably meant ' 帐户(常量帐户&),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40388998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com