enumerated type and lvalue missing

This is a discussion on enumerated type and lvalue missing within the C++ Programming forums, part of the General Programming Boards category; I've writen some program in borland C++ builder X the want to compile it in builder 6 and VS C++ ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Question enumerated type and lvalue missing

    I've writen some program in borland C++ builder X the want to compile it in builder 6 and VS C++ 2005 and i've got such problem in both compilers i get such error

    "error C2676: binary '++' : 'TerrainType' does not define this operator or a conversion to a type acceptable to the predefined operator" in VS

    and

    "[C++ Error] Modedit.cpp(33): E2277 Lvalue required" in C++ Builder 6

    for this part of code
    Code:
    for (TerrainType terrain=Plains;terrain<=Village;terrain++)
    where TerrainType is enumerated type
    Code:
    enum TerrainType
    {
      Plains=0,
      Forest,
      Hills,
      Swamp,
      Water,
      Village
    };
    so how to make this loop work properly i've tried "+1" instead of "++" but then VS still shows errors and Builder compiles but program starts an infinit loop.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Unfortunately you cannot increment an enum. Try using an int instead as the type for terrain.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Hmmm.Ok i'll use int. Funny thing is that borland C++ builder X is compiling it and C++ builder 6 not. I've also found somebooks and tutorial that says it's correct code.That's why i'm puzzeld.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by baniakjr
    Hmmm.Ok i'll use int. Funny thing is that borland C++ builder X is compiling it and C++ builder 6 not. I've also found somebooks and tutorial that says it's correct code.That's why i'm puzzeld.
    And what compiler should do with things like
    Code:
    enum flags
    {
      red = 1,
      blue = 2,
      green = 4,
      black=8,
     ...
    }
    ???
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    By some means, you should be able to overload the ++ operator for the enum, so that any horrid casting is well hidden from normal users.

    Consider this
    Code:
    enum TerrainType
    {
      Plains=0,
      Forest = 0,
      Hills = -42,
      Swamp = 666,
      Water = 8,
      Village = 1000
    };
    Naive casting of ++ is going to produce illegal enum values.
    enums are allowed to have disjointed sequences and identical values for different symbols, which is why you don't get an automatic ++ operator.

    <edit: beaten>
    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.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    I see the point. Thats true. But still i've tried that in borland C++ builder X and when i tried to enumerat lik this

    Code:
    enum TerrainType
    {
      Plains=0,
      Forest = 0,
      Hills = -42,
      Swamp = 666,
      Water = 8,
      Village = 1000
    };
    i've got error but not in enumeration but in loops using it.

    and i've tried

    Code:
    enum flags
    {
      red = 1,
      blue = 2,
      green = 4,
      black=8
    }
    than it's perfectly compiling it just like puting them in rising order according to value and than make correct step. Maybe builder X have some modified iostream library with overloaded ++ operator.

    In any case thanks for help.no i see why it didn't work in VS and builder 6. And it sounds resonable And i wonder if i should use it or no in builder X nevertheless it's compiling and working correct.

    p.s. I've also found a book in which i saw it. It's "C++ Pocket Refrence" published by O'Reilly.

    there is such code sample:
    Code:
    enum SpectrumColor
    {
        Red,Orange,Yellow,Green,Blue,Indigo,Violet
    };
    
    for (SpectrumColor s=Red;s<=Violet;s++)
    {
       ...
    }

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    If the enumerators are in sequence, it makes sense to use it inside a for, and you see no reason for the enumeration to become anything else in the future, do it.

    Cast to int. Don't overload ++ though.

    Code:
    enum TerrainType
    {
      Plains=0,
      Forest,
      Hills,
      Swamp,
      Water,
      Village,
      NUM_TERRAINTYPES
    };
    
    for (unsigned int i = Plains; i != NUM_TERRAINTYPES; ++i) {
    
    }
    If you see one reason whatsoever why maybe TerrainType will one day not be in sequence, then you shouldn't do this.

    EDIT: Corrected ugly code error
    Last edited by Mario F.; 11-22-2006 at 06:12 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM

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