vector iterators incompatible error

2019-07-20 13:58发布

问题:

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)) );

回答1:

The error is in code that you do not show. The following code, which I have written based on your code and your descriptions, does not produce any errors:

#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>

using namespace std;

class Deck
{
};

class Player()
{
  public:
    Player(Deck *d) {}
};

class Human : public Player
{
  public:
    Human(Deck *d, string n) : Player(d), name(n) {}
  private:
    string name;
};

class House
{
  public:
    House(Deck *d) {}
};

int main()
{
    Deck deck;
    vector<Human> humans;
    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 = House(&deck);
    return 0;
}


回答2:

Instead, try

iter != names.end()


回答3:

Always pre-increment iterators in a for loop and use it != end as your sentinel in C++ (this also goes for ints.

for(iter = names.begin(); iter **!=** names.end(); **++iter**)


回答4:

The Microsoft <vector> header implementation has some debugging checks enabled in your build that make iterators much richer objects than mere pointers - they keep track of the container they're iterating through and their 'neighbor' iterators as well as the object they're pointing to. The assertion you're running into is checking that 2 iterators that should be pointing into the same container, but it's finding that they don't (according to the iterators' state).

So you're either corrupting the iterator objects/lists somewhere or you're doing something that isn't shown in your code snippets.