# Problems with vector.push_back()

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

## Recommended Posts

Hello everybody, Yesterday I started to program Black Jack game in console and I have problems with vectpr.push_back(), I use it to add cards from deck to players hand, for what I use CPlayer::PDraw(CDeck d) function, here it is:

void PDraw( CDeck d)
{
cards.push_back(d.getDeck()[cardCounter]);//error, deck deletes somehow after doing one command
cardCounter++;
}


The problem is, when I use puch back(), my deck what I pass into function somehow is changed and becomes empty, I think it has to do something with my Copy and Assignment constructors of CDeck class, here is my CDeck class:

//Class representing Deck of Cards
class CDeck
{
public:
CDeck(){
pDeck= new SCard[CARD_NUMBER];
for(int i = 0; i < CARD_NUMBER; i ++)
{
//Assign ID to card and its suit and value
pDeck.id = i;
identifyCard(&pDeck);
}
}
~CDeck(){
delete[] pDeck;
}
CDeck(const CDeck &d) { operator=(d); }//error here?
const CDeck &operator=(const CDeck &d) {//still mistake
for(int i = 0; i < CARD_NUMBER; i++)
{
pDeck.id = d.pDeck.id;
pDeck.type = d.pDeck.type;
pDeck.num = d.pDeck.num;
pDeck.val = d.pDeck.val;
}
return d;
}
void shuffleDeck()
{
//Shuffles Deck, but maybe more suitable with vector and rand()
std::random_shuffle(pDeck, pDeck + 52);
}
void identifyCard(SCard* c)
{
c->type = suits[c->id/13];
c->val = value[c->id%13];
c->num = nums[c->id%13];
}

SCard* getDeck(){return pDeck;}
private:
SCard* pDeck;
};


Here is SCard structure:

//Structure representing one card
typedef struct
{
//Card ID convertible to its value and suit
int id;
//Card Value from Sce to King
string val;
//Card suit, Hearts etc.
string type;
//Cars numerical value with ace being 11
int num;
}SCard;


ANd lastly, my CPlayer class:

//Class representing main Player
class CPlayer
{
public:
CPlayer()
{
bet = 0;
balance = START_MONEY;
counter = 0;
//party begins
isPlaying = true;
//first move is banks
isActive = false;
}
//Player actions funcs
void PStay()
{
isActive = false;
}
void PDraw( CDeck d)
{
cards.push_back(d.getDeck()[cardCounter]);//error, deck deletes somehow after doing one command
cardCounter++;
}
void PDouble(CDeck d)
{
PDraw(d);
bet*=2;
}
void PMove(CDeck d)
{
int choise;
cout << "Choose move what you like: \n1.Stay.\n2.Draw\n3.Double.\n";
cin >> choise;
if(choise == 1) PStay();
if(choise == 2) PDraw(d);
if(choise == 3) PDouble(d);
}
void PInfo()
{
cout << "Balance: " << balance << endl;
cout << "Bet: " << bet << endl;
cout << "Cards in your hand:" << endl;
vector<SCard>::iterator iter;
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
cout << (*iter).val << " of " << (*iter).type << endl;
}
cout << "Value: " << PSum() << endl;;
}
void PBet()
{
{
cout << "Enter the bet between 10 to "<< balance << ": ";
cin >> bet;
if(bet < 10 || bet > balance)
cout << "\nYou can't bet this sum. Type Again\n";
else
}
balance -= bet;
}
int PSum()
{
int numOfAces = 0; //nuimber of aces in hand
vector<SCard>::iterator iter;
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
if((*iter).val == "Ace") numOfAces++;
counter += (*iter).num;

}
if(counter > BLACK_JACK)
{
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
if((*iter).val == "Ace")
counter -= 10;// +1 and -11
if(counter <= BLACK_JACK)
break;

}
}
return counter;
}

bool isPlaying;//player currently playing


bool isActive; // is players move
private:
int balance;
int bet;
//stores current points that player has
int counter;
//stores current cards in hand
vector<SCard> cards;
};


I regard every possible answer to this problem. When I run program and step to line where I push card into my vector representing cards in player hands it throws unhandled exception (Unhandled exception at 0x779615de in Black Jack.exe: 0xC0000005: Access violation writing location 0x76d833aa.). To be more precise it comes to constructor of CDeck and then breaks.

##### Share on other sites

I thought the same way, array is probably not the best way to implement my deck, is there any other way without changing pDeck?

##### Share on other sites

void PDraw(CDeck d) makes a copy of the deck because you are passing by value rather than by reference.

void PDraw(CDeck &d) passing by reference

Edited by hikrea

##### Share on other sites

I thought the same way, array is probably not the best way to implement my deck, is there any other way without changing pDeck?

Yes, you could allocate a new array in your copy constructor before copying from the other object.

Actually, now that I look at your code again, there doesn't seem to be any reason that you need dynamic memory at all. A regular array (or std::array) of CARD_NUMBER elements would work just as well.

##### Share on other sites
This is unrelated to the problem, but I think I should mention that using 64-bit arithmetic one can encode a set of cards (e.g., a deck or a hand) with a single number. If you are ever concerned about performance (like, if you want to develop a competitive AI engine) this format makes many operations very fast.

##### Share on other sites

Thank you everybody. Passing CDeck by reference helped, so I think I do not need to convert it to vector.

##### Share on other sites
If you decide not to fix your copy constructor you should consider disabling it. For example, declaring it private and removing the definition or, if you have access to a C++11 compiler, declaring it as = delete.

##### Share on other sites

Thank you, I removed my copy constructor and passed CDeck as reference. Now it works and im nearly finished my game

Edited by kaktusas2598

##### Share on other sites
What exactly do you mean by "removing"? Removed as per SiCrane's suggestions or just removed it from your code?

If you do not supply one explicitly the compiler will generate one which might have similar problems depending on the situation.

##### Share on other sites

What exactly do you mean by "removing"? Removed as per SiCrane's suggestions or just removed it from your code?

If you do not supply one explicitly the compiler will generate one which might have similar problems depending on the situation.

I just removed it from my code and compiled generated constructors works just fine for me. One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

##### Share on other sites

What exactly do you mean by "removing"? Removed as per SiCrane's suggestions or just removed it from your code?

If you do not supply one explicitly the compiler will generate one which might have similar problems depending on the situation.

I just removed it from my code and compiled generated constructors works just fine for me.

But as BitMaster said, the compiler generated copy constructor is as incorrect as the copy constructor you originally had. It doesn't work. Your class simply isn't set up for handling the ownership of the pointer, and neither the compiler generated copy constructor nor your original copy constructor is doing the right job. And by the way, this applies to the copy assignment operator as well.

The point is to completely disable copying by making the copy constructor and assignment operators private to ensure that you cannot copy the objects. Or, implement proper copy operators to ensure that the ownership of the pointer is handled correctly.

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.

srand(time(NULL));


##### Share on other sites

kaktusas2598, on 28 Mar 2013 - 13:09, said:

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.

srand(time(NULL));

But I am using random_shuffle(). not rand(), does srand() also works with random_shuffle()? :o

##### Share on other sites

kaktusas2598, on 28 Mar 2013 - 13:09, said:

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.

srand(time(NULL));

But I am using random_shuffle(). not rand(), does srand() also works with random_shuffle()?

Sorry, I was thinking randomness and seeding and wasn't exactly thinking about what I was actually answering. The random generator used by the two-parameter variant of std::random_shuffle is in fact implementation defined, but probably uses rand. If it does, then srand will seed std::random_shuffle.

However, you may want to use the variant with the third parameter instead, which lets you specify the random number generator yourself. I believe you can pass rand as the third parameter to make sure that it does, in fact, use rand for random numbers.

##### Share on other sites
rand() isn't a valid function for the third parameter of std::random_shuffle(). If you pass it a function, the function has to take one argument n, and return a value from 0 to n - 1. rand() doesn't take any arguments.

##### Share on other sites

I apologize, that makes total sense.

##### Share on other sites

I have read on internet, that random_shuffle really was inplemented using rand(), so srand() works! Thank you, fellow developers, for all your help!

Edited by kaktusas2598

##### Share on other sites
It would be more accurate to say "is usually implemented using rand()". The C++ standard doesn't require standard library implementations to use rand() under the hood for the two argument version of the random_shuffle() function. I would be cautious about trusting an internet source that said this was definitely the case (unless it was referring to your particular compiler/standard library implementation).

##### Share on other sites
I just checked a late draft of the C++11 standard, and SiCrane is correct. This is unfortunate, and it leaves us with two options: Either avoid using the two-argument version of random_shuffle (since the standard doesn't give you any tools to seed the source of randomness) or live our lives ignoring this oddity in the standard, and trusting that compiler writers are sensible people. If a compiler doesn't use rand as the source of randomness, I will happily stop using that compiler.