Pointer to dynamic array of pointers to objects

五迷三道 提交于 2019-12-08 07:44:24

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/9338228/pointer-to-dynamic-array-of-pointers-to-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!