gpt4 book ai didi

c++ - 错误 "has no member named"

转载 作者:行者123 更新时间:2023-11-30 02:11:41 25 4
gpt4 key购买 nike

我有这段代码

账户.cpp

#include "account.h"
#include <iostream>
#include <string>

using namespace std;

Account::Account(string firstName, string lastName, int id)
: strFirstName(firstName), strLastName(lastName), nID(id) {}

void Account::printAccount(){
cout << strFirstName;
}

账户.h

#include <string>

using std::string;

class Account{
private:
string strLastName; //Client's last name
string strFirstName; //Client's first name
int nID; //Client's ID number
int nLines; //Number of lines related to account
double lastBill;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};

公司.h

#ifndef CELLULAR_COMPANY_H
#define CELLULAR_COMPANY_H

#include <string>
#include <list>
#include <iostream>
#include "account.h"

using namespace std;

class Company {
private:
list<Account> listOfAccounts;
public:
void addAccount(string firstName, string lastName, int id) {
Account newAccount(firstName, lastName, id);
listOfAccounts.push_back(newAccount);
}

void printAccounts(){
for(list<Account>::iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
i.printAccount; //here bug
}
}
};


#endif // CELLULAR_COMPANY_H

main.cpp

#include "cellularcompany.h"

int main(){
Company newCompany;
newCompany.addAccount("Pavel", "Nedved", 11111);
newCompany.printAccounts();

return 0;
}

有人可以解释一下我的错误是什么意思吗?提前致谢(我在 company.h 那里有评论) 我有错误 'struct std::_List_iterator<Account>' has no member named 'printAccount'

最佳答案

您忘记了 printAccount() 之后的括号。否则,它不是方法调用。此外,您需要使用 -> 运算符,因为它是一个迭代器。

for(list<Account>::iterator i = listOfAccounts.begin();
i != listOfAccounts.end(); ++i)
{
i->printAccount(); // Note the ()!
// This is equivalent to (*i).printAccount();
}

关于c++ - 错误 "has no member named",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3060187/

25 4 0