Thread: compile error

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    compile error

    I am getting a compiler error (error C2106 "left operand must be l-value") in developer studio 6.0. the left side of my equation is an enum type that I cast to an int. The right side of the equation is of int type. What am I doing wrong? Thanks in advance.
    JK

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I think your problem is coming from typecasting on the left side of the equation. The left side is the assignment side - nothing should change. Give us the actual line of code and explain what you're doing and perhaops we can offer an alternative.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    instead of putting the code here, first, would you provide an example of how to properly do what I am trying to do...that is to say: I have an enum type on the left side = to an int type on the right side...example of how this is accomplished.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I have an enum type on the left side
    You need a variable (of a type) on the left hand side

    Code:
    int = 123;   // not good
    Code:
    enum_type enum_var;
    enum_var = (enum_type)123;  // better - cast int to an enum
    No doubt someone will be along in a moment with the correct C++ cast expression...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    instead of putting the code here, first, would you provide an example of how to properly do what I am trying to do
    In future, help us help you. No one appreciates having to guess what you're trying to do.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    here's the code:

    Code:
    enum ColorType_t {BLUE, GREEN, RED}
    int colors;
    
    ColorType_t ColorType;
    
    (int) ColorType = colors;

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yeah. Use Salem's solution - cast on the right side, not the left.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I tried that and received an error.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What error? And again, post the code that gave you the error.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I received this error: (error C2440, "cannot convert from enum colors to enum ColorType_t ") for the code listed below.



    Code:
    enum ColorType_t {BLUE, GREEN, RED}
    int colors;
    
    ColorType_t ColorType;
    
    ColorType = (enum) colors;

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >ColorType = (enum) colors;
    Code:
    ColorType = (ColorType_t) colors;
    enum alone can't be used as a cast.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    That worked! Prelude...you have god-like programming ability! Thanks to all for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 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