Thread: class won't accept enum in a function

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    class won't accept enum in a function

    I have this big problem with using enumerators in a class function

    Code:
    class Automat
    {
        public:
            void print();
            int AddCoins(enum Coins, int MoreCoins);
        private:
            typedef enum {One=1, Two, Three} Coins;
            int MoreCoins;
    
    --snip--
    
    Automat::AddCoins(Automat::Coins, int MoreCoins)
    {
        if (Coins == ONE)
            CoinStack += MoreCoins;
    }
    etc...
    I get this error from Eclipse
    Code:
    error: 'enum Automat::Coins' is private
    I have tried moving it around to public: and outside the class entirely.
    I also get an error on the function:
    Code:
    error: 'Automat::AddCoins(int, int)'
    error: expected identifier before ',' token
    error: C++ forbids declaration of 'AddCoins' with no type
    can someone point me in the right direction, as I am a bit lost on the syntax, as to why it doesn't accept my enums as parameters to a function

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Code:
    int AddCoins(enum Coins, int MoreCoins);
    The name "Coins" is a type, so you can't specify it as a variable name as you are here. You need to do something like this:

    Code:
    int AddCoins(Coins coins, int MoreCoins);
    The function definition should have the same parameter list, and "coins" is what you reference inside the function. Also, "Coins" should be in the public section of the class.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Thank you, solved my problem entirely.

    I was on track with the enum, but I just couldn't see that the enum-name "Coins" was a type and not a name to a type.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Roffemuffe View Post
    Thank you, solved my problem entirely.

    I was on track with the enum, but I just couldn't see that the enum-name "Coins" was a type and not a name to a type.
    A "type" and a "name to a type" are the same. Well, maybe not strictly speaking: an "int" is a name to the type "int", but can also be said to be of the type "integer". Anyways, when using typedef you make a new TYPE (yes, you make a new name/alias for a specific type, really). That doesn't mean you make a variable, as your usage seemed to imply. But then again, of course you can't use a variable as a type as you try by specifying it as a type for a first parameter in the function call.

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    What's the deal with this?

    Code:
    typedef enum {One=1, Two, Three} Coins;
    wouldn't you prefer the following instead?

    Code:
    enum Coins {One, Two Three};

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    Because the default integer values for an enumeration start at 0.
    Example:
    Code:
    enum Coins {One, Two, Three}
    Sets One = 0, Two = 1, and Three = 2. Setting One = 1 means each successive option will have an integer one higher than the previous, e.g. in this case the same numerical value as they are written.

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by iamthesgt View Post
    Because the default integer values for an enumeration start at 0.
    Example:
    Code:
    enum Coins {One, Two, Three}
    Sets One = 0, Two = 1, and Three = 2. Setting One = 1 means each successive option will have an integer one higher than the previous, e.g. in this case the same numerical value as they are written.
    I suppose. Personally I'm averse to acknowledging (let alone using) the integer association with enum elements. They're meaningless to me.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    typedef enum ... name;

    That's C style. Just write enum name.

    Your out-of-line definition of AddCoins is missing the return type.

    Finally, put the enum definition above the declaration of AddCoins. If you write this:
    Code:
    class X {
    public:
      void foo(E e);
      enum E { e1, e2 };
    };
    then a strictly conforming compiler will reject it, saying that E is unknown.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by CornedBee View Post
    then a strictly conforming compiler will reject it, saying that E is unknown.
    Nice pickup. I totally missed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM