2010-01-25 57 views
3

我一直在谷歌搜索和阅读这个,并没有拿出一个答案呢,也许有人可以帮助我这个。C++在'{'标记之前的预期类名。继承

我想我的UserPile类能够从我的CardPile类访问数据成员和类成员函数。我一直在标题中提到错误。有人能解释发生了什么吗?我见过的继承教程看起来就像我的代码,除了我的代码是多个源代码文件。

//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() 
{ 

} 

我没有什么在这个UserPlayer类情况发生,但因为我将使用从基类的功能,所以我希望至少看到它编译之前,我开始写它。

感谢任何人的帮助。

+0

请不要reedit破碎的标记... – 2010-01-25 01:33:22

+0

@gf,你和我在同一时间编辑的问题,在6秒差异提交,但SO没有通知,因为它已经编辑,但刚刚提交,我发现你比较好,我将它回滚到你的修订版,但OP再次编辑到最好的版本,所以现在应该可以。 – YOU 2010-01-25 01:36:45

+0

啊,好的。这些* some-second-diffs *有时很奇怪。 – 2010-01-25 01:40:53

回答

4

如果您想在此处使用CardPile类,则必须包含CardPile.hUserPlayer.h

你还缺少包括在头警卫,例如:

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

class CardPile { 
    // ... 
}; 

#endif 

没有这个你实际上包括CardPile.h两次UserPlayer.cpp - 通过线#include "CardPile.h"

+0

当我这样做时,我得到重新定义班'CardPile'错误 – Isawpalmetto 2010-01-25 01:34:51

+0

你错过了包括警卫,补充说。 – 2010-01-25 01:37:12

+0

这给了我错误“unterminated #ifndef” 我只是将它添加到CardPile的顶部,对吧? – Isawpalmetto 2010-01-25 01:44:09

1

UserPlayer.h从UserPlayer.h各一次需要#include CardPile.h - 你的代码是这种情况吗?