Thread: Vector out of range error...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    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
    968
    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
    968
    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 09: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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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, 04:09 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM