2014-10-30 43 views
-1

我的程序有問題。迭代器使用關於卡

錯誤是「函數調用缺少參數列表」。

我正在製作一個卡片程序來顯示卡片。我不知道如何正確使用迭代器。

void Deck::ShowDeck() 
    { 
     vector<Card>::iterator dealt; 

     for (dealt = cards.begin(); dealt != cards.end(); dealt++) 
     { 
      cout<<*dealt<<" "; 
     } 
    } 

這裏是我加入deck.h 所以請你給我更多具體的答案?我改變了 '*處理' 到 'dealt->海峽',但它仍然是錯誤的 是的#ifndef _DECK_H_ 的#define _DECK_H_

#include "card.h" 
#include <vector> 

/** 
* This class represents a standard 52-card poker deck 
*/ 
class Deck 
{ 
public: 
    /** 
    * Default constructor. This will initialize and shuffle the deck 
    */ 
    Deck(); 

    /** 
    * Reinitilaize the deck and shuffle it. 
    */ 
    void Shuffle(); 

    /** 
    * Dump out the contents of the current deck, minus already dealt cards 
    */ 
    void ShowDeck(); 

    /** 
    * Return a vector of cards that are dealt. 
    * @param count number of cards to deal 
    * @return a set of cards from the front of the deck 
    */ 
    std::vector<Card> Deal(int count); 


private: 
    std::vector<Card> cards; 
    std::vector<Card>::iterator dealt; 
    std::vector<Card> deal; 


/* Your code here */ 

}; 

#endif //_DECK_H_ 

這是card.cpp

#include "card.h" 
#include <sstream> 

using namespace std; 

Card::Card() 
{ 
} 



std::string Card::str() 
{ 
    ostringstream os; 
    os << valToStr() << suitToStr(); 
    return os.str(); 
} 



std::string Card::suitToStr() 
{ 
    switch(suit) 
    { 
    case 1: 
     return"C"; 
     break; 
    case 2: 
     return"D"; 
     break; 
    case 3: 
     return"H"; 
     break; 
    case 4: 
     return"S"; 
     break; 
    default: 
     break; 
    } 
} 

std::string Card::valToStr() 
{ 
    switch (value) 
    { 
     case 2: 
      return "TWO"; break; 
     case 3: 
      return "THREE"; break; 
     case 4: 
      return "FOUR"; break; 
     case 5: 
      return "FIVE"; break; 
     case 6: 
      return "SIX"; break; 
     case 7: 
      return "SEVEN"; break; 
     case 8: 
      return "EIGHT"; break; 
     case 9: 
      return "NINE"; break; 
     case 10: 
      return "TEN"; break; 
     case 11: 
      return "JACK"; break; 
     case 12: 
      return "QUEEN"; break; 
     case 13: 
      return "KING"; break; 
     case 14: 
      return "ACE"; break; 
     default: break; 
    } 
} 

/* Your code here */ 

這是卡。 h

#ifndef _CARD_H_ 
#define _CARD_H_ 

#include <string> 
/** 
* Card class. This class represents a single playing card 
*/ 

class Card 
{ 
public: 
    /** 
    * Suits of playing cards 
    */ 
    typedef enum SUITS { CLUBS = 1 , DIAMONDS, HEARTS, SPADES } SUITES; 

    /** 
    * The value of the card: 2 through Ace 
    */ 
    typedef enum COUNT { TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } COUNT; 

    /** 
    * Default constructor 
    */ 
    Card(); 



    /** 
    * Constructor to initialize our suit and value 
    * 
    * @param _suit enum of SUITS 
    * @param _val value of the card (COUNT enum) 
    */ 
    Card(Card::SUITS _suit, Card::COUNT _val) 
    { 

    this->suit=_suit; 
    this->value=_val; 

    } 


    /** 
    * Get a string representation of the card. 
    * @return string in the format similar to this: "10D", which would be 10 
    * of diamonds 
    */ 
    std::string str(); 

    /** 
    * Accessor to get this card's suit 
    * @return value of this card's suit 
    */ 
    SUITS getSuit() 
    { 
     return suit; 
    } 


    /** 
    * Accessor to get this card's value 
    * @return value of this card 
    */ 
    COUNT getValue() 
    { 
     return value; 
    } 


private: 
    Card::SUITS suit; 
    Card::COUNT value; 
    std::string suitToStr(); 
    std::string valToStr(); 


/* Your code here */ 


}; 

#endif // _CARD_H_ 
+0

這段代碼有什麼問題?目前還不清楚它做了什麼以及它應該做什麼。 – kraskevich 2014-10-30 18:25:01

+1

您向我們展示的代碼段沒有任何問題。錯誤發生在哪裏? – Cameron 2014-10-30 18:26:00

+1

錯誤可能是由於您的代碼沒有爲'Card'類定義'operator <<'。 – 2014-10-30 19:37:44

回答

1

迭代器只是一個用來遍歷卡片元素的對象(不管是什麼,你沒有完全指定)。您可以訪問一個成員,你與迭代器遍歷像這樣的對象:

void Deck::ShowDeck() 
{ 
    vector<Card>::iterator dealt; 

    for (dealt = cards.begin(); dealt != cards.end(); dealt++) 
    { 
     cout<<dealt->suitToStr<<" "; // equivalent to (*dealt).cardType 
    } 
} 

請再具體些,我可以編輯這個答案,以便更好地幫助你。

+0

謝謝,但你能更具體嗎?我改爲dealt-> cardType,但它仍然錯誤我現在已經添加了一些代碼 – 2014-10-30 19:42:45

+1

「Card」究竟是什麼?直到我知道卡會員的身份,我才能幫到你。 – 2014-10-30 19:52:19

+0

我已經添加了card.cpp和card.h。謝謝你的幫助。 – 2014-10-30 20:01:10