Visual studio compiler errors at enum - c++

I have the following code which compiles without errors under Linux and Mac OS X. I now want to compile the code with Windows, but I get a lot of errors in the following code segment:
...
enum Type
{
UPDATE = 0, DELETE = 1
};
...
The error messages are these:
1>Request.hpp(48) : error C2143: syntax error : missing '}' before '('
1>Request.hpp(48) : error C2059: syntax error : '<L_TYPE_raw>'
1>Request.hpp(49) : error C2143: syntax error : missing ';' before '}'
1>Request.hpp(49) : error C2238: unexpected token(s) preceding ';'
What did I wrong, I am really confused, as this compiles without errors under Linux.
What can cause this errors?

The solution is quite easy, but one need to find out that DELETE is a Macro defined in the windows header.
I now added #undef DELETE and it works fine.

Type is an existing class. You need to change the name or specify that it's a literal.
You could try enum #Type to specify a literal, though you may need to prefix it with # elsewhere.

Related

Rewrite ERROR constant in C++

When trying to compile the following C++ code snippet (using Microsoft Visual Studio 2015):
namespace NS
{
const int ERROR = -1;
};
It returns the following error:
c:\workspace\X.hpp(141): error C2143: syntax error: missing ';' before 'constant'
c:\workspace\X.hpp(141): error C2059: syntax error: 'constant'
The constant ERROR is already defined somewhere, since if one makes the line a comment, ERROR can still be printed (it returns 0 for that matter).
I thought this type of collision could be avoid by defining a namespace.
How can one defined his/her own constant ERROR?

enum class: does not name a value error

I have
enum class ErrorLevel
{
VERBOSE,
DEBUG_,
INFORMATION,
WARNING,
ERROR
};
This works:
assertDetectionParameters( parameterSet, ErrorLevel::WARNING );
This does not:
assertDetectionParameters( parameterSet, ErrorLevel::ERROR );
Error 1 error C2589: 'constant' : illegal token on right side of '::'
Error 2 error C2059: syntax error : '::'
Resharper says:
"ErrorLevel does not name a value"
I get this error for certain names in my enum.
ERROR is probably already a preprocessor macro somewhere. Try changing it to something else, e.g. ERROR_, in order to test this hypothesis. Alternatively run your code through the preprocessor to see what substitutions are being made (gcc -E ... or whatever the equivalent is in Visual Studio).

Visual C++ compiler, errors compiling stdlib.h

I am trying to compile a file which requires functions included from cmath. I do not believe my code itself to be at fault for causing this error, I suspect the configuration (it persists even with all of the non-include code commented), but have no idea what is going wrong. I am compiling on a Windows host, 64 bit. Other than adding the masm build configuration, I don't believe I've changed any compile settings to non-default, and the problem persists after disabling masm, and by setting the target configuration to 32 bit. Here is my generated output:
1>...\VStudio\VC\include\stdlib.h(467): error C2062: type 'long' unexpected
1>...\VStudio\VC\include\stdlib.h(467): error C2062: type 'long' unexpected
1>...\VStudio\VC\include\stdlib.h(467): error C2059: syntax error : ')'
1>...\VStudio\VC\include\stdlib.h(468): error C2143: syntax error : missing ';' before '{'
1>...\VStudio\VC\include\stdlib.h(468): error C2447: '{' : missing function header (old-style formal list?)
1>...\VStudio\VC\include\stdlib.h(471): error C2062: type '__int64' unexpected
1>...\VStudio\VC\include\stdlib.h(471): error C2062: type '__int64' unexpected
1>...\VStudio\VC\include\stdlib.h(471): error C2059: syntax error : ')'
1>...\VStudio\VC\include\stdlib.h(472): error C2143: syntax error : missing ';' before '{'
1>...\VStudio\VC\include\stdlib.h(472): error C2447: '{' : missing function header (old-style formal list?)
Any help would be greatly appreciated.
Edit:
I have found the troublesome line. Contrary to my expectation, the error does occur in the code, and is the fault of a line inherited from a previous version:
#define abs(a) ((a)<0? -(a):(a))
is responsible for my problems.
Next time, #include system headers before any of your own code. That way your macros can't mess with the system headers and lead you astray.

How do you make an in-place construction of a struct casted to array compile in Visual C++ 2008?

I'm working with quite a big codebase which compiles fine in linux but vc++ 2008 spits errors.
The problem code goes like this:
Declaration:
typedef float vec_t;
typedef vec_t vec2_t[2];
The codebase is littered with in-place construction like this one:
(vec2_t){0, divs}
Or more complex:
(vec2_t){ 1/(float)Vid_GetScreenW(), 1/(float)Vid_GetScreenH()}
As far as I know, this code constructs a struct, then converts it to an array and passes the address to the function. I personally never used in-place construction like this so I have no clue how to make this one work.
The compiler produces a bunch of syntax errors like these:
Error 2 error C2143: syntax error : missing ')' before '{'
Error 3 error C2059: syntax error : ')'
Error 4 error C2143: syntax error : missing ';' before '{'
I don't maintain the linux build, only the windows one. And I can't get it to compile. Is there some switch, some macro to make vc++ compile it?
Maybe there is a similar nifty way to construct those arrays and pass them to the functions in-place that compiles just fine in vc++?
You're using a GCC extension that MSVC simply doesn't support, "compound literals", also called "constructor expressions" in older GCC docs.
If you want portable code, I think you'll need to change the code to declare the structs normally and initialize them with initializers that have constants expressions or using standard assignments (or use something like MinGW as your Windows compiler, if that'll do the trick).
I believe this is a language extension, supported by GCC that probably isn't available in MSVC. You can look around for a "Constructor Expression" in your docs, but your only solution might be to get a GCC for windows. See:
Constructor Expressions
If you simply want to compile your codes under Windows and it doesn't have to use MSVC, have you tried using GCC? it might be caused by specific extensions of GCC.
typedef float vec_t;
typedef vec_t vec2_t[2];
vec_t Vid_GetScreenW()
{
return 600.f;
}
vec_t Vid_GetScreenH()
{
return 300.f;
}
int main() {
vec_t divs = 3.f;
//The codebase is littered with in-place construction like this one:
vec_t* vec1 = (vec2_t){0, divs};
//Or more complex:
vec_t* vec2 =
(vec2_t){ 1/(float)Vid_GetScreenW(), 1/(float)Vid_GetScreenH()};
return 0;
}
The snippet above compiles with g++ (MinGW) under Windows XP and produce the following error using cl.exe:
E:\tmp>g++ vec.cc
E:\tmp>cl vec.cc Microsoft (R) 32-bit
C/C++ Optimizing Compiler Version
14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation.
All rights reserved.
vec.cc vec.cc(16) : error C2059:
syntax error : '{' vec.cc(16) : error
C2143: syntax error : missing ';'
before '{' vec.cc(16) : error C2143:
syntax error : missing ';' before '}'
vec.cc(19) : error C2059: syntax error
: '{' vec.cc(19) : error C2143: syntax
error : missing ';' before '{'
vec.cc(19) : error C2143: syntax error
: missing ';' before '}'

Some errors in VC++

I have a code that generates errors on my PC but does't on other PC.I have windows 7 installed and also installed the compatible SDK for windows 7.I have also included the VC directories (from Tools -> option-> vc Diretories) but still the code generates follwing errors.
error C2059: syntax error : 'constant'
error C2238: unexpected token(s) preceding ';'
error C2589: '(' : illegal token on right side of '::'
warning C4091: '' : ignored on left of 'const unsigned int' when no variable is declared
error C2143: syntax error : missing ';' before '::'
error C2059: syntax error : '::'
error C2059: syntax error : 'constant'
error C2238: unexpected token(s) preceding ';'
statement generating error are as following
static const unsigned int MAPVK_VK_TO_CHAR;
const unsigned int KeyLogger :: MAPVK_VK_TO_CHAR = 0x02;
Try selecting MAPVK_VK_TO_CHAR and hitting F12 to see if the symbol is declared elsewhere.
What's MAPVK_VK_TO_CHAR? I ask because the error might indicate that this is some macro.
Why do you make a plain identifier (not a macro) all caps, anyway? That's just begging for one of the thousands of macros in <windows.h> to trample over it. (Not that not making it all caps would prevent that, since <windows.h> sacrilegiously defines many mixed-case macros...)
Otherwise you would have to paste some minimal, compilable example showing the problem.