views:

310

answers:

2

I've been googling and reading about this and didn't come up with an answer yet, maybe someone can help me with this.

I want my UserPile class to be able to access data members and class member functions from my CardPile class. I keep getting the error mention in the title. Could someone explain what is happening? The inheritance tutorials I have seen look just like my code except mine is multiple source code files.

//CardPile.h
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Card;
class CardPile
{
   protected:
       vector<Card> thePile;
       //Card thePile[];
       int pileSize;
   public:
       CardPile();
       void insertCard( Card );
       Card accessCard( int);  
       void displayPile();
       void shuffle(); //shuffle the pile
       void initializeDeck(); //create deck of cards

       void deal(CardPile &, CardPile &);
       void showHand();
       bool checkForAce();
       void discard(CardPile);
       void drawCard(CardPile &);

 };

    //UserPlayer.h
 using namespace std;



class UserPlayer: public CardPile
{
    private:
        //CardPile userPile;                          
    public:
        UserPlayer(); 

};

//UserPlayer.cpp

#include "UserPlayer.h"
#include "CardPile.h"


UserPlayer::UserPlayer()
{

}

I don't have anything happening in this UserPlayer class yet, because I will be using functions from the base class, so I want to at least see it compile before I start writing it.

Thanks for anyone's help.

+1  A: 

You have to include CardPile.h in UserPlayer.h if you want to use the class CardPile there.

You are also missing include guards in the headers, e.g.:

// CardPile.h:
#ifndef CARDPILE_H
#define CARDPILE_H

class CardPile {
    // ...
};

#endif

Without this you are effectively including CardPile.h twice in UserPlayer.cpp - once from UserPlayer.h and once via the line #include "CardPile.h"

Georg Fritzsche
When I do that I get a redefinition of class 'CardPile' error
Isawpalmetto
You are missing include guards, added that.
Georg Fritzsche
this gives me the error "unterminated #ifndef"I just add it to the top of CardPile, right?
Isawpalmetto
Did you also add the `#endif` to the end of `CardPile.h`? ... See e.g. here for what include guards are: http://en.wikipedia.org/wiki/Include_guard
Georg Fritzsche
Thank you, I did not see the #endif and have not learned about include guards yet. This works now.
Isawpalmetto
+1  A: 

UserPlayer.h needs to #include CardPile.h -- is that the case with your code?

fbrereto