Question

I am creating an Uno game and I am stuck trying to search through an a vector of objects by specific attributes. It crashes when the program reaches the Game::player_selection() method. Other than the , everything is working.

#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

}

Error given

Error message I receive

Edit

Just posting here for future reference regarding find_if checks and multiple copies of vectors. So the solution was:

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
}
Was it helpful?

Solution

The problem is that get_hand is returning a vector by value, so the 2 calls to get_hand are creating different vectors. You could return a reference to the vector, or just call get_hand once:

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

You should also check the result of both calls to find_if to make sure they actually found an item satisfying the predicate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top