-1

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{

public:

myHand(){
    this->size=0;
    //this->jon={};

}

void Draw(Card anyCard) {

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

    jon[size]=&anyCard;
    cout<<"HERE IS ANYCRD:"<<jon[size]->getRank()<<jon[size]->getSuit()<<endl;
    size++;
}

void showHand() {
   //DOESNT WORK HERE
}

void Place(Card* anyCard) {

}
private:
    int size;
    Card* jon[12];
};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Dax Durax
  • 1,607
  • 5
  • 23
  • 31

3 Answers3

5

Your problem is dangling pointers.

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 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
  • 3
    @DaxDurax: [That's complete nonsense](https://ideone.com/giMXoX). 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](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) 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
  • 2
    Even better, use a `std::vector`. 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
  • @DaxDurax: You're completely and totally wrong. Also, pointers are objects. – Puppy Jan 29 '13 at 19:52
  • 1
    @us2012: It really is! :D – Lightness Races in Orbit Jan 29 '13 at 19:53
  • @us2012, No, they invented `std::random_shuffle` for that :) – chris Jan 29 '13 at 19:55
1

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...

jon[size]=&anyCard;

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

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 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
  • @Non-StopTimeTravel: Sorry, overlooked that, you and chris are correct of cours.e – Ed S. Jan 29 '13 at 19:55
0

You are storing the address of a local variable:

 jon[size]=&anyCard;

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.

pbhd
  • 4,384
  • 1
  • 20
  • 26