C++ pass enum as parameter

53,265

Solution 1

Use Card::Suit to reference the type when not inside of Card's scope. ...actually, you should be referencing the suits like that too; I'm a bit surprised that Card.CLUBS compiles and I always thought you had to do Card::CLUBS.

Solution 2

Suit is part of the class Card's namespace, so try:

Card::Suit suit = Card::CLUBS;
Share:
53,265

Related videos on Youtube

Spencer
Author by

Spencer

Updated on December 09, 2020

Comments

  • Spencer
    Spencer about 2 years

    If I have a simple class like this one for a card:

    class Card {
            public:
                enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
                Card(Suit suit);
        };
    

    and I then want to create an instance of a card in another file how do I pass the enum?

    #include "Card.h"
    using namespace std;
    int main () {
        Suit suit = Card.CLUBS;
        Card card(suit);
        return 0;
    }
    

    error: 'Suit' was not declared in this scope

    I know this works:

    #include "Card.h"
    using namespace std;
    int main () {
        Card card(Card.CLUBS);
        return 0;
    }
    

    but how do I create a variable of type Suit in another file?

    • mingos
      mingos over 12 years
      WTF, Card.CLUBS doesn't give you an error??? I'd think it needs to be Card::CLUBS... x_x
  • kapilddit
    kapilddit over 10 years
    use Card.CLUBS as it will match with question.

Related