空矢量< ...>没有成员的名字

时间:2017-04-24 01:17:25

标签: c++ vector

我试图打印矢量内容并获得以下内容

错误消息aka class __gnu_cxx::__normal_iterator<const SoccerTeams*, std::vector<SoccerTeams> >}’ has no member named ‘teamName’

这是我的班级

class SoccerTeams {
    string teamName;
 public:
    vector<SoccerTeams> teams;
    void addTeam(string name) {
        SoccerTeams newTeam(name);
        teams.push_back(newTeam);
    };
    void showTeams() {
        cout << "\nHere's all the teams!";

        //error here
        for (vector<SoccerTeams>::const_iterator i = teams.begin(); i != teams.end(); ++i) 
            cout << *i.teamName << endl;
    }
    SoccerTeams(string tn){
        teamName = tn;
    };
    ~SoccerTeams(){};
};

我认为错误存在是因为矢量团队目前是空的,有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:5)

.运算符的优先级高于一元*。因此*i.teamName*(i.teamName),尝试在teamName而不是const_iterator对象SoccerTeams中查找成员*i

您需要(*i).teamName,或等效i->teamName