0

I am trying to make a class representing a card. but i faced this error while compiling.

error: expected `;' before "suit".

Can anyone help what is problem with this code?

#include<string>
using namespace std;
class Card {

private:
       Card::Suit suit;
       Card::CardName cardName;
public:
    enum Suit {Clubs, Diamonds, Hearts, Spades, UNKNOWN_SUIT};
    enum  CardName {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, UNKNOWN_CARDNAME};
    class NotInitalised : public exception{};
    Card ();
    Card (int card);
    Card (Card::Suit s, Card::CardName n);
    void setCard(Card::Suit, Card::CardName);
    Card::Suit getSuit();
    Card::CardName getCardName();
    Card::CardName getCardName(int);
    int getCardValue();
    string toString();
};
3
  • You're inside the same scope where the enums were declared. Therefore, the types don't need scope resolution. Commented Oct 30, 2012 at 7:26
  • By the way, it works when you move the enums to the top of the class: liveworkspace.org/code/67cd31d07367763b0e2d5c4de5ae09cd Commented Oct 30, 2012 at 7:30
  • 3
    Unrelated: break yourself of that using namespace std; in a header file right now. the potential side effects are not worth the convenience (and there is no convenience). Use namespace-qualified ids/types in your headers. (ex. std::string) Commented Oct 30, 2012 at 7:31

3 Answers 3

2

As always in C++ you need to declare something before you use it. You must therefore move the definition of enum Suit before the declaration of the suit member.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to move the enum definitions before the point you use them. For instance something like:

#include<string>
using namespace std;
class Card {

public: 
    enum Suit {Clubs, Diamonds, Hearts, Spades, UNKNOWN_SUIT};
    enum  CardName {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, UNKNOWN_CARDNAME};
private:
       Card::Suit suit;
       Card::CardName cardName;
public:
    class NotInitalised : public exception{};
    Card ();
    Card (int card);
    Card (Card::Suit s, Card::CardName n);
    void setCard(Card::Suit, Card::CardName);
    Card::Suit getSuit();
    Card::CardName getCardName();
    Card::CardName getCardName(int);
    int getCardValue();
    string toString();
};

should work.

Also you don't need to qualify the uses of the enums inside your class definition, i.e. instead of

Card::Suit suit;

you can use

Suit suit;

I usually declare all the public elements of a class before the private ones, since I want to have the public interface of the class when I look at it, but this is just a style preference that others may not agree with.

Comments

0

Remove scope resolution operator and move type declaration before use of that type

You class will look like as follows:

using namespace std;
class Card {

private:
    enum Suit {Clubs, Diamonds, Hearts, Spades, UNKNOWN_SUIT};
    enum  CardName {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, UNKNOWN_CARDNAME};

     Suit suit;
     CardName cardName;
public:
    class NotInitalised : public exception{};
    Card ();
    Card (int card);
    Card (Suit s, CardName n);
    void setCard(Suit, CardName);
    Suit getSuit();
    CardName getCardName();
    CardName getCardName(int);
    int getCardValue();
    string toString();
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.