To check objects inside std::array has identical member data

115 views Asked by At

Cards.h

class Card
        {
        public:

            // Card suits
            struct Suit
            {
                // Suits in order
                enum Enum
                {
                    Clubs,
                    Diamonds,
                    Hearts,
                    Spades,
                };
            };

            // Card rank
            struct Rank
            {
                // Ranks with aces low
                enum Enum
                {
                    Ace,
                    Two,
                     King,
                     ....
                      ...
                };
            };

// constructors 
//get & set functions

//predicate

 friend bool compareCardsSuit(const Card & cardlhs, const Card & cardrhs)
 {
      return cardlhs.GetSuit() == cardrhs.GetSuit();
 }

friend bool operator==(Card const& lhs, Card const& rhs) // THis func is used for some other purpose
            {
                // We only care about rank during equality
                return lhs.m_rank == rhs.m_rank;
            }

hands.h

class Hand
        {
        public:
            static int const Size = 5;

            // The type of hand we have
            struct Type
            {
                // Type of hand in accending order
                enum Enum
                {
                    HighCard,// Five cards which do not form any of the combinations below 
                    Flush,  // Five cards of the same suit
                    bla,
                    bla..

                };
            };

        // Object creation
        // other functiosn



private:
       mutable  std::array<Card, Size>          m_cards;    
        Type::Enum                              m_type;                 
        // Hand calculations
        Type::Enum                              Evaluate();

hands.cpp

    Hand::Type::Enum Hand::Evaluate()
    {

     std::equal(m_cards.begin(), m_cards.end(), compareCardsSuit); // got error 
     {
         return Hand::Type::Flush;
     }
     // Return our hand
     return Hand::Type::HighCard;
   }

All I want is to check that if member data of m_cards has identical suit then return flush..

I am getting error as shwon below

Error 3 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion)

Error 2 error C2171: '++' : illegal on operands of type 'bool (__cdecl *)(const Card &,const Card &)'

1

There are 1 answers

0
Ivan Aksamentov - Drop On BEST ANSWER

To check for a specific suit you could use std::all_of

const bool areAllClubs = std::all_of(m_cards.cbegin(), m_cards.cend(), 
    [](const Card& card) { 
          return card.GetSuit() == Card::Suit::Clubs; 
    }));

To check all adjacent cards are verifying some criteria you could use std::adjacent_find

const auto it = std::adjacent_find(m_cards.cbegin(), m_cards.cend(), 
    [](const Card& left, const Card& right) { 
        return left.GetSuit() != right.GetSuit(); 
    });
    if (it == m_cards.end()) {
         // All cards have same suit
    }
    else {
         const auto& card = *it; // Points to a first card mismatched
    }

or simply

    const auto it = std::adjacent_find(m_cards.begin(), m_cards.end());

Last one will use operator==(const Card&, const Card&) as a predicate

P.S. Code above is written by heart in the default SO text editor and has never been compiled. Sorry for possible typos.