简体   繁体   中英

vector iterators incompatible error

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

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

相反,尝试

iter != names.end()

始终在for循环中预递增迭代器,并将it != end用作C ++中的哨兵(这也适用于int

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM