Linked Lists error: m_deck undeclared identifier
My compiler's throwing a fit because m_deck is an undeclared identifier. But I think I DID declare it: Please tell me what you think from the following snippets:
I have declared card data structures in a card.h header file.
I have declared a list of cards as a nonmember function in my deck.h header file.
Code:
typedef std::list<s_card> cardList_t;
and I have declared a cardList_t named m_deck in my game.h header file.
Then I use it in my game.cpp definition file.
Code:
void shuffleDeck()
{
cardNodeptr cur = head;
int x, y;
for(int count=1; count<=52; count++)
{
x = 1+rand()%52;
y = 1+rand()%52;
m_deck.insert(y,m_deck.retrieve(x));
m_deck.removeCard(x);
}
}
Thanks as always for your time!
Shuffling Cards Using Old-Fashioned Linked Lists
Thanks for that! Okay, so I cleaned it all up. Actually I scrapped it and started over from stubs. Now I am attempting to shuffle a deck of cards, and have an issue. Code first, explanation of issue after:
Header:
Code:
#pragma once
#include "card.h"
class CDeck : public CCard
{
public:
CDeck();
bool empty();
void insert(int index, s_card c);
void insertAtHead(s_card c);
void retrieve(int index, s_card c); // <- This is the function in question
void remove(int index);
void buildDeck();
void shuffleDeck();
protected:
int size();
};
Partial Definition:
Code:
void CDeck::insert(int index, s_card c)
{
if(empty()||index==1)
{
insertAtHead(c);
}
else
{
cardNodeptr cur = head;
int nodenum = 1;
while(cur != NULL && nodenum < index)
{cur = cur->next;}
nodenum++;
}
}
void CDeck::retrieve(int index, s_card c)
{
cardNodeptr cur = head;
for(int x=1; x<=index; x++)
{cur = cur->next;}
c = cur->item;
}
void CDeck::remove(int index)
{
cardNodeptr cur = head;
cardNodeptr prev = NULL;
if(index==1)
{head = head->next;}
else
{
for(int x=1; x<=index; x++)
{
prev = cur;
cur = cur->next;
}
}
delete cur;
}
// and the problematic bloc:
void CDeck::shuffleDeck()
{
int x,y;
for(int count=1; count<=52; count++)
{
x = 1+rand()%52;
y = 1+rand()%52;
insert(y,retrieve(x,));//<- What goes in retrieve's second parameter?
remove(x);
}
}
I suspect I am not using pointers correctly. If I were to call retrieve() from main(), I could just stick an object in there. But I'm not doing that. So what goes in there? How do I tie these pieces together?
Unhandled exception caused by LL build deck
Unhandled exception has a problem with my buildDeck function below
Code:
void CDeck::buildDeck()
{
cardNodeptr cur = head;
for(int x=1; x<=4; x++)
{
for(int y=1; y<=13; y++)
{
if(x==1)
{cur->item.suit = 'H';}
else if(x==2)
{cur->item.suit = 'S';}
else if(x==3)
{cur->item.suit = 'D';}
else if(x==4)
{cur->item.suit = 'C';}
cur->item.value = y;
cur->next;
}
}
}
because value, suit, and next cannot be evaluated.
Now, the card structure is defined in a different class. Do I need to combine classes or is there a better method of access?