[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 730
  • Last Modified:

C++ list class and pointers

I am doing a card simulation and have a card and deck class. I am trying to make a new deck (list of cards) and swap the current one with the new one. In the midst of that, making the new list, I can't seem to get the iterator functions to work with pointers. Can I get some help?
//header
#include <iostream>
#include <list>
 
using namespace std;
 
namespace SimModels {
 
class Card
{
private:
 
 
 
public:
	//Card();
	char color;
	char word;
	Card(char myColor,  char myWord);
	void setColor(char newColor);
	char returnColor();
	
};
 
class Deck
{
private:
 
 
public:
	list<Card> cards;
	Deck();	//initialize deck at beginning of round
	Deck(int whatever);
	//Deck(Card first);	// new discard pile during game
	//Deck[] reshuffle();	// reshuffling when 1 card
	void Randomize(); // shuffle new deck
};
 
} //SimModels
 
//main functions
 
void Deck::Randomize()
{
	Deck *tempdeck = new Deck(0);
	int listsize = this->cards.size();
	list<cards>::iterator it;	//pointer
 
	//iterate to max list size
	for (int i=0; i<listsize; i++)
	{
		// set position to remove card in deck
		int position = rand() % this->cards.size();
 
		advance (it, position);
		//read it
		cout << (Card)*it.returnColor() << endl;
 
		//cout << &it << endl;
		//tempdeck->cards.push_front(it);
		
 
		//erase it
		//this->cards.erase(position);
	}
}
 
void main()
{
	Deck myDeck;
	cout << myDeck.cards.size();
	myDeck.Randomize();
	//cout << mylist.size();
 
}

Open in new window

0
allenlo77
Asked:
allenlo77
1 Solution
 
jkrCommented:
Seems to be two simple issues, try the following and check the comments:
void Deck::Randomize()
{
        Deck *tempdeck = new Deck(0);
        int listsize = this->cards.size();
        list<Card>::iterator it;       //pointer <------- typo, 'Card', not 'cards'
 
        //iterate to max list size
        for (int i=0; i<listsize; i++)
        {
                // set position to remove card in deck
                int position = rand() % this->cards.size();
 
                advance (it, position);
                //read it
                cout << it->returnColor() << endl; // this is the correct syntax
 
                //cout << &it << endl;
                //tempdeck->cards.push_front(it);
                
 
                //erase it
                //this->cards.erase(position);
        }
}

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now