I'm getting a vector iterators incompatible error during runtime. the line where it happens is at the very end of the code section, inside the for loop (humans.push_back( Human(&deck, (*iter)) );) When I first got the error, I was using a different iterator than 'iter' by mistake, so the runtime error totally made sense. But now that I changed it and recompiled everything (I double checked that), I still get this error.
void BlackjackGame::getHumansAndHouse()
{
// asks how many players, pushes_back vector accordingly, initializes house, checking for valid input throughout
string input;
vector<string> names;
while(true)
{
cout << "How many humans? (1 - 7)" << endl;
cin >> input;
if(!isdigit(input[0]))
cout << "Invalid input. ";
else
{
input.erase(1);
int j = atoi(input.c_str());
for(int i = 1; i <= j; i++)
{
while(true)
{
cout << "Enter player " << i << " name: ";
cin >> input;
if(strcmp(input.c_str(), "House") == 0)
cout << "Player name has to be different than 'House'." << endl;
else
{
names.push_back(input);
break;
}
}
}
break;
}
}
vector<string>::iterator iter;
for(iter = names.begin(); iter != names.end(); iter++)
humans.push_back( Human(&deck, (*iter)) );
house = House(&deck);
}
humans is a vector:
vector<Human> humans;
where Human is a class whose constructor is as follows:
Human(Deck *d, string n) : Player(d), name(n) { printNameCardsAndTotal(); }
(Human is a derived class of Player)
since iter is an iterator to a vector of strings, I don't understand why I get vector iterators incompatible in that line inside the for loop. It's not like I'm trying to use iter directly with humans.
error is here:
humans.push_back( Human(&deck, (*iter)) );