Solved From Ubuntu to Windows: Syntax Error C2059 / C2143
-
I just copy my working Qt project from my Ubuntu 20.04 machine to my Windows 10 x64 machine.
I got two weird compile time errors in my custom header file:
#ifndef MESSAGE_TYPES_H #define MESSAGE_TYPES_H typedef enum { ERROR = -2, // C2059: syntax error: 'constant' WARNING = -1, STATUS = 0 } MSG_TYPE; // C2143: syntex error: ';' missing before '}' #endif // MESSAGE_TYPES_H
Why it's happening?
How can I fix it?My systems are:
A)- Ubuntu 20.04 x64
- Qt Creator 4.12.4
- Qt 5.15
- GCC 9.3 Compiler x64
B)
- Windows 10 x64
- Qt Creator 4.12.4
- Qt 5.15
- MSVC 2019 x64
-
Hi
Its a name clash with MSVC 2019
ERROR is defined already. -
Hi
Its a name clash with MSVC 2019
ERROR is defined already. -
@mrjj Thank you....now its working good!
Second doubt: I was thinking that 'ERROR' inside the custom
enum
will not clash with MSVC 2019.
Why this happen? -
@fem_dev said in From Ubuntu to Windows: Syntax Error C2059 / C2143:
Why this happen?
Because it's already defined as @mrjj already told you. It's a #define .
-
@fem_dev
ERROR
will be defined in a header file, either an MSVC-specific one or somewhere in some Windows header file. It might be defined in COM. -
You have the option of using mingw in windows (basically gcc for windows). This will help avoid these kinds of problems. YMMV.
-
@fcarney said in From Ubuntu to Windows: Syntax Error C2059 / C2143:
This will help avoid these kinds of problems. YMMV.
No, not in this case. ERROR is a define in a windows header.
btw: using uppercase in enums is ... strange -
@fem_dev You can use "enum class" to avoid name clashes:
typedef enum class { ERROR = -2, // C2059: syntax error: 'constant' WARNING = -1, STATUS = 0 } MSG_TYPE;
But then you will need to qualify using enum name:
MSG_TYPE::ERROR;
-
@Christian-Ehrlicher said in From Ubuntu to Windows: Syntax Error C2059 / C2143:
btw: using uppercase in enums is ... strange
Sadly its mandatory - at least the first letter - if you want to expose your enum to the QML-Engine
-
@J-Hilk
In fairness, the usual way now for anenum
is initial capital followed by smalls, which should avoid the calls withERROR
. -
Hi
Just a note.
I seen many C(not ++) coding standards that say to use all caps for enum values
over the years to follow all caps for constants. -
@mrjj
Indeed, that's why I wrote " the usual way now". I think it has changed so thatenum
constants are like class names. Certainly if you look at https://doc.qt.io/qt-5/qt.html you see all Qt enum values are done that way for their values. -
@jsulm said in From Ubuntu to Windows: Syntax Error C2059 / C2143:
But then you will need to qualify using enum name:
MSG_TYPE::ERROR;Again: this will not help. It's a define in wingdi.h:
/* Region Flags */ #define ERROR 0 #define NULLREGION 1 #define SIMPLEREGION 2 #define COMPLEXREGION 3 #define RGN_ERROR ERROR
-
About wingdi.h clobbering the global #define-space, Microsoft usually provides an escape hatch, put this
#define NOGDI
somewhere before #include windows.h