I'm trying to create a deck of cards by iterating over the enums Suit
and Rank
(I know there's no great way to iterate over enums but I don't see an alternative). I did this by adding an enumerator enum_count
to the end of each enum, whose value is meant to represent the length and end of the enum.
#include <vector>
using namespace std;
enum class Suit: int {clubs, diamonds, hearts, spades, enum_count};
enum class Rank: int {one, two, three, four, five, six, seven, eight,
nine, ten, jack, queen, king, ace, enum_count};
struct Card {
Suit suit;
Rank rank;
};
class Deck{
vector<Card> cards{};
public:
Deck();
};
Deck::Deck() {
// ERROR ON THE BELOW LINE
for (Suit suit = Suit::clubs; suit < Suit::enum_count; suit++) {
for (Rank rank = Rank::one; rank < Rank::enum_count; rank++) {
Card created_card;
created_card.suit = suit;
created_card.rank = rank;
cards.push_back(created_card);
};
};
};
However, when I try to loop over the enum, the compiler doesn't like that I'm trying to increment the suit++
and rank++
in the for-loop, stating:
card.cpp|24|error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]|
card.cpp|25|error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]|
What is the best way to go about creating a deck of cards without throwing away the useful enum data structures?