簡體   English   中英

向量迭代器不兼容錯誤

[英]vector iterators incompatible error

在運行時出現矢量迭代器不兼容錯誤。 發生錯誤的行在代碼部分的最后,在for循環內(humans.push_back(Human(&deck,(* iter)));)當我第一次遇到錯誤時,我使用的迭代器不同於錯誤地使用了“ iter”,因此運行時錯誤完全有意義。 但是,現在我更改了它並重新編譯了所有內容(我再次檢查了該內容),仍然收到此錯誤。

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

人類是向量:

vector<Human> humans;

其中Human是一個類,其構造函數如下:

Human(Deck *d, string n) : Player(d), name(n) { printNameCardsAndTotal(); }

(人類是玩家的派生類)

由於iter是字符串向量的迭代器,因此我不明白為什么在for循環內的那一行中會得到不兼容的向量迭代器。 並不是我想直接將它用於人類。

錯誤在這里:

humans.push_back( Human(&deck, (*iter)) );

錯誤出現在您未顯示的代碼中。 我根據您的代碼和描述編寫的以下代碼不會產生任何錯誤:

#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**)

Microsoft <vector>標頭實現在您的構建中啟用了一些調試檢查,這些調試檢查使迭代器比單純的指針豐富得多的對象-它們跟蹤通過其進行迭代的容器,其“鄰居”迭代器以及所要對象指向。 您遇到的斷言是檢查應該指向同一個容器的2個迭代器,但是發現它們沒有指向(根據迭代器的狀態)。

因此,您正在破壞某個地方的迭代器對象/列表,或者正在執行未在代碼段中顯示的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM