向量迭代器不兼容错误 [英] vector iterators incompatible error

查看:68
本文介绍了向量迭代器不兼容错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时遇到向量迭代器不兼容错误.它发生在代码部分的最后,在 for 循环内 (humans.push_back( Human(&deck, (*iter)) );)当我第一次遇到错误时,我错误地使用了与iter"不同的迭代器,因此运行时错误完全有道理.但是现在我更改了它并重新编译了所有内容(我仔细检查了它),我仍然收到此错误.

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

人类是一个向量:

vector<Human> humans;

其中 Human 是一个类,其构造函数如下:

where Human is a class whose constructor is as follows:

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

(Human 是 Player 的派生类)

(Human is a derived class of Player)

因为 iter 是一个字符串向量的迭代器,我不明白为什么我在 for 循环内的那一行中得到不兼容的向量迭代器.我并不是想直接与人类一起使用 iter.

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.

错误在这里:

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

这篇关于向量迭代器不兼容错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆