迭代器使用卡片

时间:2014-10-30 18:23:19

标签: c++ iterator

我的程序出了问题。

错误是"函数调用缺少参数列表"。

我正在制作卡片节目以显示卡片。我不知道如何正确使用迭代器。

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

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

这是我的deck.h 你能不能给我更具体的答案?我改变了'处理过'&#39;到&#39;处理 - &gt; str&#39;但它仍然是错误的     #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 */

这是card.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_

1 个答案:

答案 0 :(得分:1)

迭代器只是一个用于遍历Card元素的对象(不管是什么,你都没有完全指定)。您可以使用迭代器访问正在遍历的对象的成员,如下所示:

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

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

请更具体,我可以编辑此答案以更好地为您提供帮助。