gpt4 book ai didi

c++ - 在另一个类中调用一个类函数(非静态成员函数的无效使用)

转载 作者:行者123 更新时间:2023-11-28 04:31:15 30 4
gpt4 key购买 nike

<分区>

我正在创建代表银行账户的第一个类。第二类代表注册表,使用一个 vector 来存储第一类的对象。如您所见,我在 main 中调用一个函数,该函数调用第二个类中的函数,该函数调用第一个类中的函数。

using namespace std;

class Account {
private:
string name;
double balance;
char allowNegative;

public:
Account();
void setName(string holderName);
void setAllowNegative(char ynNegative);
void setBalance(double balance);
string getName() const;
double getBalance() const;
bool yesNegative() const;
};

Account::Account() {
balance = 0;
}

class Registry {
private:
vector<Account> accounts;

public:
bool exists(string holderName);
void addAcount(Account currAcount);
Account getAccount(string holderName) const;
};

void Account::setName(string holderName) {
name = holderName;
}

void Account::setAllowNegative(char ynNegative) {
allowNegative = ynNegative;
}

void Account::setBalance(double transaction) {
balance = balance + transaction;
}

string Account::getName() const {
return name;
}

double Account::getBalance() const {
return balance;
}

bool Account::yesNegative() const {
if (allowNegative == 'y') {
return true;
}
return false;
}

bool Registry::exists(string holderName) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts.at(i).getName == holderName) {
return true;
}
}
return false;
}

void Registry::addAcount(Account currAcount) {
accounts.push_back(currAcount);
}

Account Registry::getAccount(string holderName) const {
Account tempAccount;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.at(i).getName == holderName) {
tempAccount = accounts.at(i);
break;
}
}
return tempAccount;
}

主要是我有这部分:

if (!regCopy.exists(holderName)) {
throw runtime_error("account does not exist");
}

regCopy 是 Registry 类的一个对象

我收到了这个错误: enter image description here

我看到了很多关于这个问题的链接,其中大部分都包含使用指针,但我还没有看到。所以我避免使用它。

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