Vector out of range error...

This is a discussion on Vector out of range error... within the C++ Programming forums, part of the General Programming Boards category; The part where I do this: show_hand.append(values[hand[loop].Value]); Is giving me trouble during runtime... For some reason I can't see the ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Vector out of range error...

    The part where I do this:
    show_hand.append(values[hand[loop].Value]);
    Is giving me trouble during runtime...

    For some reason I can't see the problem with my code, maybe you can?
    Code:
    	string read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < hand.size(); loop++)
    		{
    			show_hand.append(values[hand[loop].Value]);
    			show_hand.append(" of ");
    			show_hand.append(suits[hand[loop].Suit]);
    			show_hand.append("		");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    values is a typedef std::vector<string> lookup.
    and so is suits..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Given that hand.size() is in the loop condition, I would suspect that the problem is hand[loop].Value, rather than loop.

    Perhaps print the hand[loop].Value ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I can do the bold line of code, but not the italic one.
    Code:
    	string read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < hand.size(); loop++)
    		{
    			std::cout << hand[loop].Value << std::endl;
    			std::cout << values[hand[loop].Value].c_str() << std::endl;
    			show_hand.append(values[hand[loop].Value]);
    			show_hand.append(" of ");
    			show_hand.append(suits[hand[loop].Suit]);
    			show_hand.append("		");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what is the Value printed on that line, and what do you expect it to be? Or, put another way, what is values.Size()?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I initialize the lookups in the overidden virtual function initialize declared in the base class.
    Code:
    class Hand : public Data
    {
    	void draw(Card card)
    	{
    		hand.push_back(card);
    	}
    
    	std::vector<Card> get_cards()
    	{
    		return hand;
    	}
    
    	string read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < hand.size(); loop++)
    		{
    			std::cout << hand[loop].Value << std::endl;
    			std::cout << values[hand[loop].Value].c_str() << std::endl;
    			show_hand.append(values[hand[loop].Value]);
    			show_hand.append(" of ");
    			show_hand.append(suits[hand[loop].Suit]);
    			show_hand.append("		");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    
    	void initialize()
    	{
    		add_to_lookup(suits, "Clubs");
    		add_to_lookup(suits, "Diamonds");
    		add_to_lookup(suits, "Hearts");
    		add_to_lookup(suits, "Spades");
    		add_to_lookup(values, "Two");
    		add_to_lookup(values, "Three");
    		add_to_lookup(values, "Four");
    		add_to_lookup(values, "Five");
    		add_to_lookup(values, "Six");
    		add_to_lookup(values, "Seven");
    		add_to_lookup(values, "Eight");
    		add_to_lookup(values, "Nine");
    		add_to_lookup(values, "Ten");
    		add_to_lookup(values, "Jack");
    		add_to_lookup(values, "Queen");
    		add_to_lookup(values, "King");
    		add_to_lookup(values, "Ace");
    	}
    	
    private: // lookups
    	friend class Dealer;
    	friend class Game;
    	lookup suits;
    	lookup values;
    	string show_hand;
    	vector<Card> hand;
    };
    The base class:
    Code:
    class Data // this is the class that will define how we output certain data sets on the screen
    {
    public:
    	typedef std::vector<string> lookup;
    	// everytime we create a class that is derived from Data it will run this function
    	Data()
    	{
    		initialize();
    	}
    
    	void add_to_lookup(lookup table, string word)
    	{
    		table.push_back(word);
    	}
    
    	virtual void initialize(){}// function to initialize a specific lookup.
    	virtual void read_data(){} // function to output specific data.
    };
    Last edited by Shamino; 11-27-2007 at 08:15 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, that didn't answer my question. You are trying to index to "hand[loop].Value", and I wanted to know what that value is in relation to values.Size(). Ok?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Well, I figured since add_to_lookup adds items to the lookup values, you'd see that values.size() should = 13, two three four five six seven eight nine ten jack queen king ace

    Since initialize should be called in the constructor.

    OH OH,

    And value is whatever random card number the player is delt here:
    Code:
    	void Deal(int howmanycards, Player & player)
    	{
    		if (player.playerid == 0)
    		{
    			for (int loop = 0; loop != howmanycards; loop++) 
    			{
    				hand.draw(deck.cards.back());
    				if (loop == 1)
    				{	
    					deck.cards.pop_back();
    				}
    				else
    				{
    				deck.cards.pop_back();
    				}
    
    			}
    		}
    		else if (player.playerid !=0)
    		{
    			for (int loop = 0; loop != howmanycards; loop++) 
    			{
    				player.hand.draw(deck.cards.back());
    				std::cout << player.hand.read_cards().c_str() << std::endl;
    				deck.cards.pop_back();
    			}
    		}
    	}
    Last edited by Shamino; 11-27-2007 at 08:32 AM. Reason: my bad i didn't mean to sound like a dick lol
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Oops, I misread again I guess, I updated that reply lol.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Anyone? This is really holdin me back here lol.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > void add_to_lookup(lookup table, string word)
    If you don't pass table as a reference, it will never be updated:
    Code:
    	void add_to_lookup(lookup &table, string word)

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Orly?

    Okay, so I changed this:
    Code:
    class Data // this is the class that will define how we output certain data sets on the screen
    {
    public:
    	typedef std::vector<string> lookup;
    	// everytime we create a class that is derived from Data it will run this function
    	Data()
    	{
    		initialize();
    	}
    
    	void add_to_lookup(lookup & table, string word)
    	{
    		table.push_back(word);
    	}
    
    	virtual void initialize(){}// function to initialize a specific lookup.
    	virtual void read_data(){} // function to output specific data.
    };
    But do I have to do something different when I call initialize?
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    #include "Data.h"
    #include "Deck.h"
    #include "IO.H"
    class Hand : public Data
    {
    	void draw(Card card)
    	{
    		hand.push_back(card);
    	}
    
    	std::vector<Card> get_cards()
    	{
    		return hand;
    	}
    
    	string read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < hand.size(); loop++)
    		{
    			std::cout << hand[loop].Value << std::endl;
    			std::cout << values[hand[loop].Value].c_str() << std::endl;
    			show_hand.append(values[hand[loop].Value]);
    			show_hand.append(" of ");
    			show_hand.append(suits[hand[loop].Suit]);
    			show_hand.append("		");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    
    	void initialize()
    	{
    		add_to_lookup(suits, "Clubs");
    		add_to_lookup(suits, "Diamonds");
    		add_to_lookup(suits, "Hearts");
    		add_to_lookup(suits, "Spades");
    		add_to_lookup(values, "Two");
    		add_to_lookup(values, "Three");
    		add_to_lookup(values, "Four");
    		add_to_lookup(values, "Five");
    		add_to_lookup(values, "Six");
    		add_to_lookup(values, "Seven");
    		add_to_lookup(values, "Eight");
    		add_to_lookup(values, "Nine");
    		add_to_lookup(values, "Ten");
    		add_to_lookup(values, "Jack");
    		add_to_lookup(values, "Queen");
    		add_to_lookup(values, "King");
    		add_to_lookup(values, "Ace");
    	}
    	
    private: // lookups
    	friend class Dealer;
    	friend class Game;
    	lookup suits;
    	lookup values;
    	string show_hand;
    	vector<Card> hand;
    };
    
    class Player
    {
    public:
    	int playerid;
    	Hand hand;	
    };
    
    
    #endif
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I just realized that initialize wasn't being called when I created the hand even though the base class Data has the contstructor already defined, I defined a Hand() constructor and now it works.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Yes usually when you get that error it is because your vector is either non existent or you are indeed out of range. First few times I got this error because of the vector not being valid I was quite confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21