Stijn
Stijn

Reputation: 305

can't perform a for loop with iteration in c++

I am developing a games library where I can add or delete my games. So far I just made a list with 3 games in standard, but when i was trying to test it. It gave me an error.

I am trying to do it with an iteration on my vector, but for some reason it won't work. And i can't find that reason.

here's my code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{

vector<int>::const_iterator iter;

vector<string> games;
games.push_back("Crysis 2");
games.push_back("God of War 3");
games.push_back("FIFA 12");

cout <<"Welcome to your Games Library.\n";
cout <<"These are your games:\n";
for (iter = games.begin(); iter != games.end(); ++iter)
{
    cout <<*iter <<endl;
}

return 0;

}

Upvotes: 0

Views: 353

Answers (3)

CapelliC
CapelliC

Reputation: 60014

your iterator doesn't match the container type: declare this way

vector<string>::const_iterator iter;

Upvotes: 2

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

You are trying to iterate a string vector with an int const iterator. Change your iterator type.

Upvotes: 0

mark
mark

Reputation: 7667

the types of you iterator and your vetcor are incompatible.

use:

vector<string>::const_iterator iter;

To make things easier it might be better to typedef your collection type:

typedef std::vector<std::string> GamesListType;
GamesListType::const_iterator iter;
GamesListType games;

Upvotes: 4

Related Questions