【问题标题】:Receiving the "vector iterators incompatible" error收到“向量迭代器不兼容”错误
【发布时间】:2014-04-28 02:29:14
【问题描述】:

我正在创建一个 Uno 游戏,但我一直在尝试按特定属性搜索对象向量。当程序到达 Game::player_selection() 方法时它会崩溃。除了 ,一切正常。

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Card {
public:
  string get_colour();
  string get_type();
};

class Player {
public:
  vector<Card*> get_hand(); //Should this be a reference?
private:
  vector<Card*>current_cards;
};


int main() {
srand(time(NULL)); //Makes shuffle more random (different every time)
Game my_game;
Player new_player("Hank", "human");
Player jack_player("Jack", "human");
my_game.add_player(new_player); //Must create players before doing other tasks
my_game.add_player(jack_player); //Must create players before doing other tasks
my_game.setup_game();
my_game.display_players();

cout << "enter colour" << endl;
cin >> colour;
cout << "enter type" << endl;
cin >> type;

my_game.play_card_to_pile(my_game.player_selection("Jack", colour, type));
my_game.display_players();

Game.cpp

Card* Game::player_selection(string p_name, string colour, string type){
    vector<Player>::iterator p_iter;
    vector<Card*>::iterator c_iter;
    p_iter = find_if (game_players.begin(), game_players.end(), [&] (Player& p) -> bool{return p.get_name() == p_name;}); //Finds correct player
    c_iter = find_if(p_iter->get_hand().begin(), p_iter->get_hand().end(), [&] (Card*& c) -> bool{return c->get_colour() == colour && c->get_type() == type;}); //Finds correct card

    return (*c_iter);//Should return correct card

}

给定错误

编辑

仅在此处发布以供将来参考有关 find_if 检查和向量的多个副本。所以解决方案是:

Card* Game::player_selection(string p_name, string colour, string type){
    vector<Player>::iterator p_iter;
    vector<Card*>::iterator c_iter;
    p_iter = find_if (game_players.begin(), game_players.end(), [&] (Player& p) -> bool{return p.get_name() == p_name;});
    if (p_iter != game_players.end()){
        vector<Card*> hand = p_iter->get_hand();//Otherwise have multiple copies of the vector stored in different places in memory
        c_iter = find_if(hand.begin(), hand.end(), [&] (Card*& c) -> bool{return c->get_colour() == colour && c->get_type() == type;});
        if (c_iter != hand.end()){
            return (*c_iter); //If found return found Card
        }
    }
    return (get_pile_card()); //Else return a default
}

【问题讨论】:

    标签: c++ pointers object vector reference


    【解决方案1】:

    问题在于get_hand 按值返回vector,因此对get_hand 的2 次调用正在创建不同的向量。您可以返回对向量的引用,或者只调用一次get_hand:

    vector<Card*> hand = p_iter->get_hand();
    c_iter = find_if(hand.begin(), hand.end(), ...
    

    您还应该检查对find_if 的两次调用的结果,以确保它们确实找到了满足谓词的项目。

    【讨论】:

    • 在进行了建议的更改后(仅调用 get_hand() 一次),我现在得到错误:vector iterator not dereferencable。您认为这可能是什么原因造成的?
    • 可能是我回答的第二部分。通过检查 c_iter != hand.end() 来检查你是否真的找到了一个项目
    • 非常好,感谢您的及时帮助。 3 小时后,我现在可以使用它了:D 搞砸了 lambdas 和内存。
    猜你喜欢
    • 2014-07-18
    • 2012-04-17
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多