2010-01-25 44 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 - 你的代碼是這種情況嗎?

相關問題