Answer the question
In order to leave comments, you need to log in
How to implement a queue of class objects in C++?
The essence of the program: the game "Drunkard".
Main task: to implement a queue of game cards.
My map class:
class card {
public:
card() {
next = NULL;
}
void setValue(int i); // Записывает значние в карту
int getValue(); // Получает значение карты
void printCard(); // Вывод карты
void setNext(card &adr); // Записать адрес следующей карты
card* getNext(); // Получить адрес следующей карты
private:
int value; // Значение карты
card *next; // Адрес следующей карты
};
class player {
public:
player() {
}
void enqueue(int value); // Добавить карту в руку игрока (в очередь)
card* dequeue(); // Получить карту из руки игрока (из очереди)
int empty(); // Остались ли у игрока еще карты
void clear(); // Очистить руку игрока
void printPlayer(); // Вывести все карты игрока (для тестирования)
private:
card *lastCard = NULL; // Ссылка на последнюю карту в очереди
card *firstCard = NULL; // Ссылка на первую карту в очереди
};
void player::enqueue(int value){
if (empty()) {
firstCard = new card;
firstCard->setValue(value);
lastCard = new card;
firstCard->setNext(*lastCard);
}
else {
card *newCard = new card;
lastCard->setValue(value);
lastCard->setNext(*newCard);
lastCard = newCard;
}
}
card* player::dequeue() {
card *tmp = firstCard;
firstCard = firstCard->getNext();
return tmp;
}
Answer the question
In order to leave comments, you need to log in
Your class player
does not contain an explicitly declared destructor. Whereas in a method void player::enqueue(int value)
you allocate dynamic memory. Most likely you will have a memory leak.
Use smart pointers, or write a destructor to clean everything up.
The method prototype itself void player::enqueue(int value)
is not architecturally correct. The argument must not be an int. Depending on what you ultimately want, you need to implement either void player::enqueue(const card&)
, or void player::enqueue(card&&)
, or both.
In general, working with raw pointers in C++ is not the best idea. Again, without knowing the final task, it's hard to say, but you need to return player::dequeue()
either a smart pointer, or a reference to an object, or the object itself. But not a pointer.
Classcard
you store information about the next card. This is again not the right architectural solution. This class should be as simple as possible. And the queue must be implemented separately. Now you have some strange spreading of the logic of the player class over two classes.
Why don't you use the standard queue
STL container?
Why create a trail. map? You have a trace in the class. the map is declared as a pointer, i.e. if the card is the last, then the pointer points to nowhere, this can also be a sign of the last card. Here is the code you create a new map then in the next field of the "old last map" write a pointer to an instance of the "new last map"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question