Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Class redefinition problem

Status
Not open for further replies.

Slagathor

Mechanical
Jan 6, 2002
129
Help!

I am teacing myself C++, and need some guidance. The project I created for myself to teach myself about OOP is a Card game simulation. I just started. Outline is a follows:

Class Card;

Private Data Members:
int Rank //2-14 to represent 2-Ace)
char Suit; //ASCII codes 0x03-0x06 for suit
unsigned long Index //this will be used to shuffle

Public Methods:
Card(); //The default constructor...assigns nulls)
int getRank(void); //returns Suit
char getSuit(void); //returns Rank
unsigned long getIndex; //returns Index
void SetCard(int rank); //Overloaded Function
void SetCard(char suit);
void SetCard(uns long index);
void SwapCards(Card A, Card B); //Swaps two cards data
void DisplayCard(); //Displays Rank, Suit and Index


Class Deck;

Private Data Members:
Deck Card[52];

Public Methods;
Deck(); //Default constructor..assigns Rank and Suits
Display(); //Displays all 52 cards
Shuffle(); //Shuffles Deck

Ive got this all working and implemented excepted for the Shuffle() method.

I have structured the progam as follow

main.cpp
Card.cpp (implementation of Card Methods)
Deck.cpp (implementation of Deck Methods)
Random.cpp (random number generation function implemtation)
QuickCardSort.cpp (Quicksort to work on object array with
an unsigned long index field in the
object)

Card.h
Deck.h
Random.h
QuickCardSort.cpp

I can call the random function, but I am having problem with QuickCardSort. The problem is that I am calling QuickCardSort from withing the Shuffle method for Deck. I do this because I want to protect the data within the deck.

Problem is that "Card.h" must be included in Deck. QuickCardSort must also have "Card.h" included as this function is tailored to the Card Class. (I am not yet far enough along to figure out how to use a Template to make my sorting fuction more generic (able to act on multiple ata types...) Problem is I now have a redifintion of the Card class, and it wont compile?

How do you get around Class Redefinition? Do I have to use a template based Sorting Function? Can I use the "stadafx" header and include "card.h" there?

Any pointers (no pun intended) would be appreciated...
 
Replies continue below

Recommended for you

Your private data members will be invisible to QuickCardSort(), so your program will not work in intended fashion. To get around this problem, you can adopt anyone of the following methods.

1. Define all data as public in classes Card and Deck.

2. Declare QuickCardSort as friend of class deck/card.

3. Define QuickCardSort within the class Deck so that QuickCardSort() becomes a deck property.

Also make all private data as protected instead of private. It generally helps and is neat.
 
Thanks for the input. QuickCardSort() is called only from within Shuffle, which is a method of Deck. But since it is an externall defined function, I guess I need to make it a friend function. I'll try it...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor