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;
}
我想要的是检查m_cards的成员数据是否具有相同的套装然后返回flush ..
我的错误如下面的
错误3错误C2678:二进制'==':找不到哪个操作符带有'Card'类型的左操作数(或者没有可接受的转换)
错误2错误C2171:'++':对类型为'bool(__ cdecl *)的操作数非法(const card&amp;,const Card&amp;)'
答案 0 :(得分:2)
要检查特定诉讼,您可以使用std::all_of
const bool areAllClubs = std::all_of(m_cards.cbegin(), m_cards.cend(),
[](const Card& card) {
return card.GetSuit() == Card::Suit::Clubs;
}));
要检查所有相邻的卡片是否正在验证您可以使用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
}
或只是
const auto it = std::adjacent_find(m_cards.begin(), m_cards.end());
最后一个将使用operator==(const Card&, const Card&)
作为谓词
P.S。上面的代码是在默认的SO文本编辑器中用心编写的,从未编译过。对不起可能的拼写错误。