Casting int to enum problem

This is a discussion on Casting int to enum problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> class Foo { public: enum Color { r, b, g }; Color c; Foo(Color cc) : c(cc) ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Casting int to enum problem

    Code:
    #include <iostream>
    
    class Foo {
    public:    
        enum Color { r, b, g };
        Color c;
        Foo(Color cc) : c(cc) {}
    };
    
    int main() {
        int i = 2;
        //Foo f(Foo::Color(i)); //function-style cast gives me an error
        //Foo f(Foo::Color(2)); //however this works
        Foo f((Foo::Color)i); //C-style cast works
        std::cout << f.c << '\n';
        std::cin.get();
    }
    From what I understand, you can cast an int to enum with a function-style cast or a C-style cast. However, the line using a function-style cast gives me the error "error C2228: left of '.c' must have class/struct/union" in Visual Studio. It works if I use a constant instead of i. Can someone explain to me why the function-style cast is giving me problems? Thanks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's because the syntax is ambigous: where you see a constructor call and a cast, the compiler sees a function declaration, basically the same as:

    Code:
    Foo f(Foo::Color i);
    That is: f is a function that takes a Foo::Color and returns a Foo.

    The second version (with 2) works, since the literal value shows this cannot be a function declaration.

    A workaround would be to use an extra pair of brackets which are not allowed in function declarations:

    Code:
    Foo f((Foo::Color(i)));
    And yet another unambigous possibility: C++ casts:

    Code:
    Foo f(static_cast<Foo::Color>(i));
    There are some cases where you run into this ambiguity, so you better be aware.

    Edit: If that is the error message, VC++ in this case words it a bit poorly. GCC is clearer in telling you what it sees, not only what it expects to see:
    16 request for member `c' in `f', which is of non-class type `Foo ()(Foo::Color)'
    But you can't expect all compiler errors to be worded clearly. With experience, and if you pay attention to the messages, you'll get used to diagnosing the causes better.
    Last edited by anon; 01-28-2009 at 10:45 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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