我正在创建代表银行帐户的第一类。第二类代表注册表,使用向量存储第一类的对象。 如您所见,我在主体中调用一个函数,该函数从第二个类调用一个函数,而第二个类调用一个函数。
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类的对象
我看到了许多有关此问题的链接,其中大多数都包含使用指针,但我还没有。所以我避免使用它。
答案 0 :(得分:1)
在exists()
的这一行上,您似乎要调用该函数,但缺少括号:
if (accounts.at(i).getName == holderName)
应该是:
if (accounts.at(i).getName() == holderName)