我试图打印矢量内容并获得以下内容
错误消息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(){};
};
我认为错误存在是因为矢量团队目前是空的,有没有办法解决这个问题?
答案 0 :(得分:5)
.
运算符的优先级高于一元*
。因此*i.teamName
为*(i.teamName)
,尝试在teamName
而不是const_iterator
对象SoccerTeams
中查找成员*i
。
您需要(*i).teamName
,或等效i->teamName
。