compile error

This is a discussion on compile error within the C++ Programming forums, part of the General Programming Boards category; I am getting a compiler error (error C2106 "left operand must be l-value") in developer studio 6.0. the left side ...

  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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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,796
    >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, 06: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

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