# STL vector help

This topic is 4486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am writing a blackjack game for a class and am having a problem splitting the hand. I have a player class with a vector of Hand classes. The Hand class holds a vector of Card class pointers. Here are my stpes: 1) create a new Hand 2) pop last card off current hand if there are 2 cards 3) add popped card to new hand (I verify that each hand has 1 card) 4) push new hand onto vector of hands (after which old hand has 0 size and new hand has size of 1) 5) house keeping I have tried pushing the new hand before adding the card and that gives a segfault. For some reason I can't add cards to the new hand after pushing it onto the vector of hands. With the original steps, if I move on and add a "second" card to the first hand and check the size I get sizes like 595 and it segfaults when accessing all those cards. I tried making a copy constructor for Hand, to copy the vector I simply said new_vector = old_vector, hoping to call the vector copy constructor. This didn't help. I don't need a copy constructor for Card because I am using pointers to the cards and not passing the Cards themselves. Any help would be greatly appreciated. Thanks

##### Share on other sites
without seeing any code i'm going to have to make assumptions.
Quote:
 Original post by StubornAHThe Hand class holds a vector of Card class pointers.

this is probably where your problem is. do you really need to use pointers? are you deleting allocated memory in a class's destructor somewhere? if so, then you may be destroying your data whenever a vector is resized. posting relevant code might help.

##### Share on other sites
what are you talking about. the problem is that when I push the second hand it screws up the vector in the second. so for the sake of argument, if the second hand holds the same card addresses as the first and the first gets destroyed would the second vector know that? I would assume that there is nothing telling the second vector that it destroy the data it points to, that is people's problem with C++.

the cards are not getting destroyed, why is the size changing?

thanks for trying though

##### Share on other sites
sorry, I may not have been clear. the vector holds addresses. as far as I know, the vector does not care if the address is valid or not. the problem is when you try to access an invalid address. since the vector does not care what it holds, why is it resizing?

##### Share on other sites
I'd try to find figure out your problem, but without source code I might as well be trying to hit a gru without a flashlight.

##### Share on other sites
(apparently I don't know how to get the code into seperate and scrollable areas)

Here is most of the Hand class implementation:

Hand::Hand(const Hand& old_hand) {  b_debug = old_hand.b_debug;  value = old_hand.value;  soft_hand = old_hand.soft_hand;  hand_cards = old_hand.hand_cards;  hand_size = hand_cards.size();  return;}int Hand::addCard (Card* new_card) {  hand_cards.push_back(new_card);  hand_size++;  if (strcmp("ace",(new_card-&gt;cardFace()).c_str()) == 0) {    if (!soft_hand) {      value += 10;      soft_hand = 1;      if (b_debug) {	    cout &lt;&lt; "Ace recieved, value added by 10." &lt;&lt; endl;      }    }  }    value += new_card-&gt;cardValue();  if (b_debug) {    cout &lt;&lt; "Value of card is " &lt;&lt; new_card-&gt;cardValue();    cout &lt;&lt; " and the value of the hand is " &lt;&lt; value &lt;&lt; endl;    cout &lt;&lt; "Gamer now has " &lt;&lt; hand_cards.size() &lt;&lt; " cards." &lt;&lt; endl;  }  if (soft_hand && (value &gt; 21)) {    value -= 10;    soft_hand = 0;  }    return 0;}int Hand::numCards () {  if (b_debug) {    cout &lt;&lt; "How many cards I got?" &lt;&lt; endl;    cout &lt;&lt; "Hand really has " &lt;&lt; hand_cards.size() &lt;&lt; " cards." &lt;&lt; endl;  }  return hand_size;}int Hand::split () {  std::vector&lt;Card*&gt;::iterator temp = hand_cards.begin();  int top_card = (*temp)-&gt;cardValue();  temp++;    if ((hand_size == 2) && ((*temp)-&gt;cardValue() == top_card)) {    cout &lt;&lt; "Okay to split." &lt;&lt; endl;    return 1;  }  cout &lt;&lt; "Not okay to split." &lt;&lt; endl;    return 0;}Card* Hand::splitHand () {  vector&lt;Card*&gt;::iterator temp = hand_cards.end();  temp--;  if (split()) {    Card* split_card = *temp;    Card* old_card;    temp = hand_cards.begin();    old_card = *temp;    //hand_cards.pop_back();    hand_cards.erase(hand_cards.begin() + 1, hand_cards.end());    if (b_debug)      cout &lt;&lt; "Hand is split and has " &lt;&lt; hand_cards.size() &lt;&lt; " cards." &lt;&lt; endl;    hand_size--;    value -= split_card-&gt;cardValue();    if (b_debug) {      cout &lt;&lt; "Hand split in splitHand() of Hand class." &lt;&lt; endl;    }    return split_card;  }  return NULL;}int Hand::printHand () {  vector&lt;Card*&gt;::iterator temp;  vector&lt;Card*&gt;::iterator last = hand_cards.end();  cout &lt;&lt; hand_cards.size() &lt;&lt; " cards: " &lt;&lt; endl;    if (hand_cards.size() &gt; 1) {    for (temp = hand_cards.begin(); temp &lt; last; temp++) {      cout &lt;&lt; (*temp)-&gt;cardFace() &lt;&lt; " of " &lt;&lt; (*temp)-&gt;cardSuit();      cout &lt;&lt; ", ";    }    cout &lt;&lt; endl;    if (soft_hand)      cout &lt;&lt; "soft";    else      cout &lt;&lt; "hard";    cout &lt;&lt; " " &lt;&lt; value &lt;&lt; endl;    return 0;  }  return -1;}

and here is the player's split function:

int Gamer::split () {  Hand* temp;  vector&lt;Hand&gt;::iterator new_hand;    if (current_hand-&gt;split()) {    temp = new Hand(b_debug);    //player_hands.push_back(*temp);  //causes segfault adding card in next line    temp-&gt;addCard(current_hand-&gt;splitHand());    //vector&lt;Hand&gt;::iterator new_hand;    //new_hand-&gt;addCard(current_hand-&gt;splitHand());  //works same as with temp    if (b_debug) {      cout &lt;&lt; "Hand split, now time to make a second hand." &lt;&lt; endl;      cout &lt;&lt; "First hand has this many cards." &lt;&lt; endl;      current_hand-&gt;numCards();      cout &lt;&lt; "Second hand has this many cards." &lt;&lt; endl;      temp-&gt;numCards();      //new_hand-&gt;numCards();    }    player_hands.push_back(*temp);  //first hand now has 0 size    if (b_debug) {      cout &lt;&lt; "First hand has this many cards." &lt;&lt; endl;      current_hand-&gt;numCards();      cout &lt;&lt; "Second hand has this many cards." &lt;&lt; endl;      temp-&gt;numCards();      //new_hand-&gt;numCards();      cout &lt;&lt; "Hand added, now move last_hand iterator." &lt;&lt; endl;      cout &lt;&lt; "Player now has " &lt;&lt; player_hands.size() &lt;&lt; " hands." &lt;&lt; endl;    }    free(temp);    last_hand = player_hands.end();    if (b_debug) {      cout &lt;&lt; "First hand has this many cards." &lt;&lt; endl;      current_hand-&gt;numCards();      cout &lt;&lt; "Last hand marked." &lt;&lt; endl;    }    return 0;  }    return -1;}

[Edited by - StubornAH on May 7, 2006 12:58:19 PM]

##### Share on other sites
Quote:
 Original post by StubornAH(apparently I don't know how to get the code into seperate and scrollable areas)
The tags that give you the scrollable source boxes are [ source ][ /source ].

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• ### Forum Statistics

• Total Topics
632160
• Total Posts
3004497

×