1 Replies - 147 Views - Last Post: 02 November 2019 - 04:43 PM Rate Topic: -----

#1 John58364   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-October 18

Linked List Deck Simulation

Posted 02 November 2019 - 04:29 PM

Deck.cpp
#include "Deck.h"
#include "Card.h"
#include <string>
#include <iostream>

using namespace std;

Deck::Deck()
{
	topCard = nullptr;
	lastCard = nullptr;
}

node* Deck::getHead()
{
	return topCard;
}

void Deck::addCard(Card card)
{
	cout << "adding" + card.printCard();
	node *tmp = new node;
	tmp->data = card;
	tmp->next = nullptr;

	if(topCard == nullptr)
	{
		topCard = tmp;
		lastCard = tmp;
	}
	else
	{
		lastCard->next = tmp;
		lastCard = lastCard->next;
	}
	cardsInDeck++;
}

void Deck::removeCard()
{
	cout << "removing" + topCard.printCard();
	cardsInDeck--;
}

void Deck::addAllCards()
{
	cardsInDeck = 52;
	int numSuits = 4;
	int numRanks = 13;
	for(int i = 1; i <= numSuits; i++)
	{
		for(int j = 1; j <= numRanks; j++)
		{
			Card temp(i, j);
			addCard(temp);
		}
	}
}

void Deck::~removeAllCards()
{
	cardsInDeck = 52;
	while(cardsInDeck != 0)
	{
		removeCard();
	}
}

string Deck::print()
{
	cout << "Deck:" << endl;
	while(cardsInDeck != 0)
	{
		cout << topCard.printCard();
		cardsInDeck--;
	}
}



Deck.h
#include <string>
#include "Card.h"

struct node
{
	Card data;
	node *next;
};

class Deck
{
public:
	Deck();
	void addCard(Card card);
	void removeCard();
	void addAllCards();
	void ~removeAllCards();
	node* Deck::getHead();
	std::string print();

private:
	int* topCard;
	int* lastCard;
	int cardsInDeck = 0;
	Card data;
};



Hi, I'm working on an assignment where I'm supposed to be simulating a deck of cards using a linked list, and the else part of my add method (addCard) is giving me some trouble. I was wondering if anyone knew why the "next" in the 'lastCard->next = tmp;' is throwing an error: Field 'next' could not be resolved. Any ideas as to why this is and any possible fixes?

Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Deck Simulation

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,225
  • Joined: 05-May 12

Re: Linked List Deck Simulation

Posted 02 November 2019 - 04:43 PM

Because integers don't have a next field. Notice on line 23 of Deck.h, that you declared lastCard to be a pointer to an integer:
int* lastCard;


If lastCard is supposed to point to a Card, then declare it as such. I suspect that you want it to be a pointer to a node though.

In the future, please post the exact error you are getting from the compiler along with relevant line numbers. Don't summarize or paraphrase like you have done above. Also post any surrounding errors and or warnings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1