Enums in Class: Is it possible to use enums defined in a class outside that class?

This is a discussion on Enums in Class: Is it possible to use enums defined in a class outside that class? within the C++ Programming forums, part of the General Programming Boards category; Hi there! I'm wondering about enums. I had defined enums in a class like this: Code: class Enums { public: ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    88

    Question Enums in Class: Is it possible to use enums defined in a class outside that class?

    Hi there! I'm wondering about enums. I had defined enums in a class like this:

    Code:
        class Enums 
        {
             public: 
                enum COLOURS {BLACK, GRAY, RED, ORANGE, YELLOW,                    GREEN, BLUE, PURPLE, BROWN, PINK, WHITE};
        }
    But I don't want to use the enums inside of that class. I want to use it in the main file like this:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    void SetColor(const int COLOR)
    {
        cout << COLOR << endl;
    }
    
    
    int main(int argc, char* argv[])
    {
        void SetColor(BLACK);
        return 0;
    }
    Without getting a syntax error. Is it any possible way to do this? Thanks for help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Enums::BLACK is probably what you want. (Note it will not print in English but as the equivalent number.)

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    88
    Yeah, I know because BLACK is actually 0 and I'm using SDL. And a switch statment to set the colour of the window to black. It would be more confusing to write 0 instead of BLACK.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Don't forget about enum class, either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  2. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  3. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  4. user-defined String class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 11:33 AM
  5. Requirements for using a user-defined class in STL
    By NixPhoeni in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2001, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21