Thread: What enum got to do with it?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    What enum got to do with it?

    Hello
    I am having this error problem see comments below.
    Can someone help me fix it, I am sure its simple but...
    Like chess I do not move until i see it and right now i see nothing wrong?!?!


    ERROR CODE:
    C:\Program Files\Microsoft Visual Studio\MyProjects\enumerators\enumerator.cpp(73) : error C2676: binary '++' : 'enum month' does not define this operator or a conversion to a type acceptable to the predefined operator


    HERE IS MY PROGRAM:
    #include<stdio.h>

    enum month {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
    typedef enum month month;

    month previous_month(month M)
    {
    switch (M)
    {
    case jan:
    M=dec;break;
    case feb:
    M=jan;break;
    case mar:
    M=feb;break;
    case apr:
    M=mar;break;
    case may:
    M=apr;break;
    case jun:
    M=may;break;
    case jul:
    M=jun;break;
    case aug:
    M=jul;break;
    case sep:
    M=aug;break;
    case oct:
    M=sep;break;
    case nov:
    M=oct;break;
    case dec:
    M=nov;break;
    }
    return M;
    }

    void print_month (month M)
    {
    switch (M)
    {
    case jan:
    printf("January");break;
    case feb:
    printf("February");break;
    case mar:
    printf("March");break;
    case apr:
    printf("April");break;
    case may:
    printf("May");break;
    case jun:
    printf("June");break;
    case jul:
    printf("July");break;
    case aug:
    printf("August");break;
    case sep:
    printf("September");break;
    case oct:
    printf("October");break;
    case nov:
    printf("November");break;
    case dec:
    printf("December");break;
    }
    }
    void main()
    {
    month M;
    printf("Table of months and their predecessors\n");
    printf("\n %16s%16s\n", "Month", "Predecessor");
    for(M = jan; M <= dec; M++) /*ERROR HERE*/
    {
    printf("");
    print_month(M);
    printf("");
    print_month(previous_month(M));
    putchar('\n');
    }
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I get the same error when I compile this code as C++, but not when I compile as C.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    I get the same error when I compile this code as C++, but not when I compile as C.

    Hello Hammer,
    According to my notes it should have worked. I am using Microsoft C++ software and every other program I have ran has worked. Strange huh?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Can't figure this one out, huh?

    Well join the club because it aint working and i have started drinkning, folks!
    PEACE OUT!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    Um, no. main returns an int, even in C++.

    In C your program works correctly because the enum identifiers are simply integers. In C++ there is a much stricter type checking so you either have to bend over backwards to increment an enum (which is an abuse of the language anyway, so such support was not included), or find some other way to do what you want. Perhaps a wrapper function or class. In case you were wondering about the bend-over-backwards way, it involves casting and may or may not be safe AFAIK:
    Code:
    for(M = jan; M <= dec; M = (enum month)( (int)M + 1 ))
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM