1

Card class header

class card {

public: 

string rtn_suit() { return suit; }
int rtn_rank()    { return rank; }
void set_suit(string new_suit){ suit = new_suit; }
void set_rank(int new_rank) { rank = new_rank; }

card();
card(string suit, int rank);
card(const card& copyCARD);
~card();

private:
string suit;
int rank;

};

#endif

card.cpp

#include "card.h"
card::card()
{
set_suit("default");
set_rank(0);
}
card::card(string new_suit, int new_rank)
{
// allows for using private class member variables
set_suit(new_suit); // can be anything (string)
set_rank(new_rank); // anticipating simple number ranking (int) 
}

card::~card() {}

Deck class header

class deck {

public:

static const int default_cap = 52;

void addCard(card *new_card);

deck & operator=(const deck &sourceDECK);

void deckPrint();

// ----------------------------------------------------

deck(int init_cap = default_cap);
deck(const deck& sourceDECK);
~deck(){ delete [] decklist ; }


private:
card *decklist;
int used;
int capacity;

};

#endif

deck class

 deck::deck(int init_cap)
 {
card **decklist = new card*[init_cap];

 for(int i=0;i<init_cap;i++)
  {
      decklist[i]=new card;
  }

capacity = init_cap;
used=0;
}

deck::deck(const deck& sourceDECK)
{
card **newDECK;

if (capacity != sourceDECK.capacity)
{

    newDECK = new card*[sourceDECK.capacity];

    for(int i=0;i<sourceDECK.capacity;i++)   {   newDECK[i]=new card();  }

    decklist = /*reinterpret_cast<card*>(*/newDECK/*)*/;

    capacity = sourceDECK.capacity;
}

used = sourceDECK.used;
copy(sourceDECK.decklist, sourceDECK.decklist+ used, decklist);

}
deck& deck::operator= (const deck& sourceDECK)
{
if (this == &sourceDECK)

    return *this;


card ** newDECK;
if (capacity != sourceDECK.capacity)
{
    newDECK = new card*[sourceDECK.capacity];

    for(int i=0;i<sourceDECK.capacity;i++)   {   newDECK[i]=new card();   }
    for (int i=0; i<capacity; i++)           {   delete     &decklist[i];     }
    delete [ ] decklist; 
    decklist = /*reinterpret_cast<card*>(*/newDECK/*)*/;
    capacity = sourceDECK.capacity;
}

// Copy the data from the source array:

used = sourceDECK.used;
copy(sourceDECK.decklist, sourceDECK.decklist + used, decklist);

return *this;
}


void deck::addCard(card* new_card)
{
//------- Not using vectors----
//deckList.push_back(new_card);
//cout << "Card added."<<endl;

decklist[used] = new_card;

//decklist[used].set_rank(new_card->rtn_rank());

//decklist[used].set_suit(new_card->rtn_suit());

++used;

cout << "Card added."<<endl;
  }

void deck::deckPrint()
 {
if ( capacity > 0 ) 
{
for(int i = 0; i < capacity; i++)
             {
               cout << "----------"<<endl;
               cout << decklist[i].rtn_rank() << " ";
               cout << decklist[i].rtn_suit() << endl;
               cout << "----------"<<endl;
             }
}
else\
{
    cout << "There are no cards in the deck."<<endl;
}
 }

And lastly a main()

int main ()
{
string new_suit, del_suit;
int new_rank, del_rank;

deck newDeck;

card *temp_card;


            cout<<"Enter the card's suit: ";
            cin>>new_suit;
            cout<<endl<<"Enter the card's rank: ";
            cin>>new_rank;

            cin.clear();
            cin.ignore(1000, '\n');


        temp_card = new card(new_suit, new_rank);

        newDeck.addCard(temp_card);


        newDeck.deckPrint();


return 0;

}

Somewhere, somehow I am not initializing or allocating properly. Or maybe I screwed up the pointers....

As is, will not compile due to:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'card *' (or there is no acceptable conversion) \projects\cards\cards\card.h(27): could be 'card &card::operator =(const card &)' while trying to match the argument list '(card, card *)'

If I use (currently commented out) a 'deeper copy' aka:

decklist[used].set_rank(new_card->rtn_rnk());

it will throw the error in deckPrint()

I originally wrote this with vectors for a simple card class implementation and then we started learning about dynamic arrays - and I got the bright idea to try and throw pointers in the mix. Again, this is not homework - this is for personal growth.

If there is a tutorial out there that I missed which clearly outlines what I am trying to do, feel free to point me towards that also - I have poured over the top 30 google results and tons of posts here. Half the time people recommend using vectors, which again is not my goal.

Thanks for your time, I know it is lengthy.

mate0x
  • 13
  • 1
  • 1
  • 4
  • 4
    Please make a *minimum size* example to ask your question. It's unlikely people will take the time to read all of that code to try to help you out. – Carl Norum Feb 18 '12 at 03:26
  • Check your `deck` copy-constructor/assignment operator. They allocate array of pointers, but do not allocate actual objects for those pointers. Compare it with `deck` constructor (which does both). Assignment operator, also, deletes array of pointers without deleting underlying objects... – lapk Feb 18 '12 at 03:38
  • @ Carl - I cut out all the fat. – mate0x Feb 18 '12 at 03:43
  • @ AzzA - I will fix those, thanks - I did not get around to my cleanup yet. Are my issues in the copy-cstr/assignment-op ? I thought they were elsewhere... – mate0x Feb 18 '12 at 03:47
  • @mate0x It's bad you deleted your copy-constructor and assignment operator - they were the source of error. If I recall correctly, after you allocated array of `card *` pointers, you were trying to copy objects `card` into it. That's why you were getting `binary '=' : no operator found which takes a right-hand operand of type 'card *'`... Follow logic of your `deck` constructor: allocate array of pointers with `new`, then for each pointer in a loop create object with another `new`. Delete in reverse order - delete objects in loop, then array of pointers. – lapk Feb 18 '12 at 03:59
  • Yea, I trimmed them out before I read your post, sorry about that! – mate0x Feb 18 '12 at 04:01
  • The best way to learn a new software technique is generally *not* to graft it into a large, complex, preexisting program. You should have written a 20-line HelloWord with a vector, then tried to convert *that* to pointers and arrays. – Beta Feb 18 '12 at 04:16
  • Beta - Nothing like diving in, and reverse engineering (even if it is your own). I do get your point though. @AzzA I updated the copy constructor and assignment op. Thanks for everyone's time, I will update it again when I am done - or if I have more issues! – mate0x Feb 18 '12 at 04:47
  • Sweet, she works great. Now to add deletion and make some more decklists! :D Thanks again everyone. – mate0x Feb 18 '12 at 04:51

1 Answers1

2

I think you have a small bug in your deck class construction function.

deck::deck(int init_cap)
{
    card **decklist = new card*[init_cap];

    for(int i=0;i<init_cap;i++)
    {
        decklist[i]=new card;
    }
}

here card **decklist is a new pointer to pointer, I guess you want to initialize the private variable decklist.

so you may change it to

deck::deck(int init_cap)
{
    decklist = new card*[init_cap];

decklist here is the private variable.

and change the declaration

private:
   card **cardlist;
TecToN
  • 103
  • 3
  • 1
    Beautiful, thank you very much! Compiles and runs! There are still errors, but you got me over the hurdle. I appreciate your time! – mate0x Feb 18 '12 at 04:42