×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

C++ Class redefinition problem

C++ Class redefinition problem

C++ Class redefinition problem

(OP)
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...

RE: C++ Class redefinition problem

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.

RE: C++ Class redefinition problem

(OP)
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...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources