Ritwick Malhotra
Ritwick Malhotra

Reputation: 97

empty vector<...> has no member named

I'm trying to print contents of a vector and get the following

Error message aka class __gnu_cxx::__normal_iterator<const SoccerTeams*, std::vector<SoccerTeams> >}’ has no member named ‘teamName’

Here's my class

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

I believe the error exists because the vector teams is currently empty, is there any way to get around this issue?

Upvotes: 3

Views: 749

Answers (1)

aschepler
aschepler

Reputation: 72271

The . operator has higher precedence than unary *. So *i.teamName is *(i.teamName), trying to look up a member teamName in the const_iterator, not the SoccerTeams object *i.

You need (*i).teamName, or equivalently, i->teamName.

Upvotes: 5

Related Questions