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.