错误“没有成员命名”

时间:2010-06-17 08:52:57

标签: c++

我有这段代码

account.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;
}

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

company.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'

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

尝试将i.printAccount;更改为i->printAccount();