Take the 2-minute tour ×

I am trying to make a class that is a players hand (in a card game for ex.). The draw method will draw another card, and showHand method should display the current cards in the hand. I have tried to initialize the array of pointers in the constructor, but am lost on how to do this (this is where I believe my issues stems from). trying the now commented out this->jon={}; gives this error: "error: incompatible types in assignment of '' to 'Card* [12]'"

***currently when in showHand if I just try to cout jon[i]->getRank() a bunch of nonsense just pops up; however Draw method works perfectly.

class myHand{




void Draw(Card anyCard) {

    if(size>11) {
        cout<<"You can only have a maximum of 12 cards in your hand at a time"<<endl;

    cout<<"HERE IS ANYCRD:"<<jon[size]->getRank()<<jon[size]->getSuit()<<endl;

void showHand() {

void Place(Card* anyCard) {

    int size;
    Card* jon[12];
share|improve this question

closed as too localized by Lightness Races in Orbit, ithcy, Ananda Mahto, Dan Moulding, Eric J. Jan 30 '13 at 2:12


jon[size]=&anyCard; - That's going to fill with junk soon after. –  chris Jan 29 '13 at 19:41
This is C++, not Java. You don't need this->. –  Thomas Matthews Jan 29 '13 at 20:22
add comment

3 Answers

up vote 5 down vote accepted

Your problem is dangling pointers.

Your wider problem is in using pointers. Just don't do that. Store an array of actual Card objects.

share|improve this answer
Every source I have seen showed that you cannot store actual objects in Arrays; only pointers to them –  Dax Durax Jan 29 '13 at 19:47
@DaxDurax: That's complete nonsense. Please provide an example of where you have seen this stated, so that I may contact the author and set them straight and ask them to fix their material. Also, get a proper C++ book from this list of proper C++ books, none of which would make such heinous lies. –  Lightness Races in Orbit Jan 29 '13 at 19:48
Even better, use a std::vector<Card>. Variable length? Check. Easily usable? Check. No worries about memory management? Check. It's like people invented std::vector for beginning programmers implementing card games! –  us2012 Jan 29 '13 at 19:51
@DaxDurax, Are you by chance referring to polymorphism? –  chris Jan 29 '13 at 19:51
@us2012: It really is! :D –  Lightness Races in Orbit Jan 29 '13 at 19:53
show 2 more comments

Arrays are not assignable, i.e., some_array = {} is illegal after the point of initialization.

Your array is already initialized at this point. It does however contain garbage, so you will need to initialize each element before using it.

On a side note...


That is bad. You are storing the address of a local variable. That pointer becomes invalid as soon as the function exits.

share|improve this answer
It's passed in by value for that function, unlike the other one. –  chris Jan 29 '13 at 19:44
You have no idea when that pointer will become invalid later. Yes, you do. It becomes invalid when control leaves that function's scope. i.e. very, very quickly. –  Lightness Races in Orbit Jan 29 '13 at 19:48
@Non-StopTimeTravel, I'm betting it's because Place takes a pointer, so that probably propagated to the Draw function. –  chris Jan 29 '13 at 19:50
@chris: Probably. If so, the OP codes "backwards". Probably stores C-strings everywhere because one API function wants std::string::c_str() as input. Regardless, the pointer is invalidated when Draw ends. –  Lightness Races in Orbit Jan 29 '13 at 19:51
@Non-StopTimeTravel, True indeed. That just seems like the likely explanation for the mixup. –  chris Jan 29 '13 at 19:52
show 1 more comment

You are storing the address of a local variable:


which gets destroyed when it goes out of scope (when the function is left). You need to make at least a copy of it if you want to store pointers in jon.

share|improve this answer
add comment
