Pointers problem

class Card{...}

class Pack
{
protected:
Card *pack;
int packSize;
int freeSlot;
public:
Pack(int S)
{
packSize = S;
pack = new Card[packSize];
freeSlot = 0;
}

Card getout(int x)
{
Card out = pack[x];
for(int i = x; i<packSize; i++)
{
pack[i] = pack[i+1];
}
packSize--;
return out;
}
}

getout function gives me bad_alloc error which seem to be connected with pointers, so i paste all about pointers here and ask of help.
It shouldn't, not how you have it written here. Maybe the problem is in your definition of "Card"? Or how you are using it? Can you paste more code?

EDIT: Since you're going through the trouble of writing a "getter", you should put a bounds check in the "getout()" function to make sure that 'x' isn't larger than the current value of 'packSize'.
Last edited on
Topic archived. No new replies allowed.