Problem with enum

This is a discussion on Problem with enum within the C Programming forums, part of the General Programming Boards category; I made 2 programs, each both use only <stdlib.h> and <stdio.h>. I can't find any important differences in them. In ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Problem with enum

    I made 2 programs, each both use only <stdlib.h> and <stdio.h>.
    I can't find any important differences in them. In the first one I did simple math problems with enum things, and it worked fine, but now in the second one, the compiler complains when i do some enum thing = 0 for example. Here is the codes from both the programs:

    program 1:
    Code:
    //this is outside of main()
    enum row {row1,row2,row3,row4,row5,row6,row7,row8,row9};
    typedef enum row row;
    
    //this is in main()
    row tmprow;
    
    for(tmprow=row1;tmprow<=row9;tmprow++)
            {}
    program 2:
    Code:
    //this is outside of main()
    enum country {italy, america, france, brazil, england, newzealand, nepal, switzerland, canada, australia};
    typedef enum country country;
    
    //this is in main()
    country tmpcountry;
    
    for(tmpcountry=0;tmpcountry<=9;tmpcountry++)
        {}
    In the first program my compiler is happy, but in the second one, he complains about each of the 3 parts of the for statement. What did I do wrong? How do I fix the 2nd program? Thanks for the help

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    This:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    enum country {italy, america, france, brazil, england, newzealand, nepal, switzerland, canada, australia};
    typedef enum country country;
    
    int main(void)
    {
        country tmpcountry;
    
        for(tmpcountry = 0; tmpcountry <= 9; ++tmpcountry)
            printf("%d\n", tmpcountry);
        return 0;
    }
    compiles fine for me under gcc with the -Werror and -pedantic flags. Although I'm not a fan of using the same name for the enum and the typedef of the enum.

    What are the errors you're getting exactly.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I am getting casting errors.

    error: invalid conversion from 'int' to 'country'

    and

    error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead
    error: no match for 'operator++' in '++tmpcountry'

    rags_to_riches: I copied your code into a nice fresh file and got the same errors again!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead
    > error: no match for 'operator++' in '++tmpcountry'
    C doesn't do operator overloading, so my guess is you're compiling your C program with a C++ compiler.

    The enum rules in C++ are stricter.
    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
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I am using Codeblocks, with GNU GCC compiler.
    And I have both the programs open in different tabs, but one compiles, and the other doesn't!

    I think that maybe the working program I copied from another computer. So maybe that computer was configured right but mine isnt?
    How do I make my compiler know I am progamming in C then? It is using GNU GCC compiler, and I am naming my files XXX.c
    Last edited by purestr999; 11-22-2010 at 10:57 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Are you running c::b on an OS that has case-sensitive filenames (like Linux)?
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main ( ) {
    #ifdef __cplusplus
      printf("Compiled as C++\n");
    #else
      printf("Compiled as C\n");
    #endif
      return 0;
    }
    $ gcc foo.c && ./a.out 
    Compiled as C
    $ gcc -x c++ foo.c && ./a.out 
    /tmp/cc7f3LHw.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    
    Now try with upper case .C
    $ cp foo.c foo.C
    $ gcc foo.C && ./a.out 
    /tmp/ccoqURep.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    $ gcc -x c foo.C && ./a.out 
    Compiled as C
    Check your compiler settings.
    Make sure you're not invoking g++ instead of gcc
    Make sure you're not passing say -x, which will change the language detection.

    Consider adding -v to see what is really going on
    Code:
    $ gcc -v foo.c
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v <<much more output snipped>>
    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.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I am using windows 7 with codeblocks 8.02 which I downloaded and installed a couple weeks ago. I didn't change any settings.

    I ran your code, and it says compiled in C++.

    By the way, I found that if I make a project, and during the wizard I chose C, then the enum code does compile. But if I make a file, and during the wizard I chose C, then it doesn't compile.

    In settings, compiler and debugging, it says GNU GCC Compiler from the drop down box.

    What do I have to do to make my programs compile in C? Can you tell me the specific setting that I should change or something? I am not that experienced in this stuff.

    I can't create all my files in projects because I need to turn these in for homework, and I am supposed to turn in just the .c file.

    I searched everywhere in the settings for how my first program which works(that I think I started making on another computer) is different from all my new programs.

    By the way, I have no idea where to type -v and -o and all that stuff, or is that only linux related?

    Thanks for the help

    P.S. lol, even if I copy the working file and paste it (not in codeblocks, i mean the actual file) the new copy won't build, it gets all the enum errors.
    Last edited by purestr999; 11-23-2010 at 06:05 AM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    can someone tell me how to change the settings so that my files are compiled in C? I am using GNU GCC in codeblocks but stuff compiles in C++

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check your compiler/IDE documentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Problem with enum and files
    By Robert_Sitter in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2005, 12:40 AM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. enum code problem
    By correlcj in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 10:00 PM

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