I have the following code:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ctime>
    #include <windows.h>
    #include <cstdlib>

    enum ERR_CODE {SUCCESS, ERROR};

But as soon as I compile it it gives this error:

C:\Users\User\Desktop\Program.cpp|8|error: expected identifier before numeric constant
C:\Users\User\Desktop\Program.cpp|8|error: expected '}' before numeric constant
C:\Users\User\Desktop\Program.cpp|8|error: expected unqualified-id before numeric constant
C:\Users\User\Desktop\Program.cpp|8|error: expected declaration before '}' token

As soon as I remove the windows.h. This is fixed. Why does it do this?

I am using Code::Blocks as my compiler.

Edited 4 Years Ago by MasterHacker110

In <windows.h> ERROR is defined as 0. So unless you were to undefine ERROR before your enum you would be writing

enum ERR_CODE { SUCCESS, 0 };

because the preprocessor just does a "find-replace" for ERROR and swaps it with 0.

A quick solution would be to just define SUCCESS as 1 so then ERROR is 0 and SUCCESS is 1. This is backwards to how you wanted it but unless you undefine ERROR or come up with a different name you cant do anything else.

The Windows API defines a macro called ERROR, which is replacing your use of ERROR with whatever value is defined. It does the same thing with TRUE and FALSE, which is why in the past I was forced to do annoying things like this:

enum boolean { B_FALSE, B_TRUE };

This is precisely the type of problem that namespaces were meant to solve. Sadly, the preprocessor is its own beast, and a beast it can be. ;)

This question has already been answered. Start a new discussion instead.