Rewrite ERROR constant in C++ - 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?

Related

Sudden compilation errors within stdlib.h, math.h, stdio.h, string.h, stdarg.h, and many others

I've downloaded a source code originally from a Code::Blocks project, in which it works absolutely fine compiling with MinGW and GCC.
I tried to load it in Visual Studio 2010 and set the includes/libraries accordingly, but I'm getting tons and tons of errors from within the standard library headers and others as well.
Mainly, they are errors like:
...\stdlib.h(314): error C2146: syntax error : missing ')' before identifier '__nptr'
...\stdlib.h(314): error C2061: syntax error : identifier '__nptr'
...\stdlib.h(314): error C2059: syntax error : ';'
...\stdlib.h(314): error C2059: syntax error : ','
...\stdlib.h(314): error C2059: syntax error : ')'
...\stdlib.h(315): error C2373: '__restrict__' : redefinition; different type modifiers
From code:
(314) strtod (const char* __restrict__ __nptr, char** __restrict__ __endptr);
(315) float __cdecl __MINGW_NOTHROW strtof (const char * __restrict__, char ** __restrict__);
And so on.
Must have screwed up big time to have such a massive amount of errors. I really have no idea where it went wrong though.

Winbase.h doesn't support in creating DLL file

I would like to create a project use to call c++ method using c#. So, I need to create a dll file for all of my c++ function. But when i get an error in y sqlite source code such as "AreFileApisASNSI undeclared identifier". So, i import winbase.h which is part of the mingw-w64 runtime package to my visual studio 2012 express DLL project. After import the file, I get many errors such as below:-
error c4430: missing type specifier - int assumed. Note: C++ does not support default - int
error C2143: syntax error : missing ';' before '_stdcall'
error c1003 error count exceeds 100; stopping compilation
error c2061: syntax error: identifier ' WINBOOL'
error c2086: 'int_CRT_INLINE' : redefinition
error c2143: syntax error: missing ';' before ''
error C2146: syntax error : missing ';' before identifier 'LONGLONG'
error C2146: syntax error : missing ';' before identifier 'PVOID'
any solution for those error? please help!!
The documentation for the function has this header requirement:
WinBase.h (include Windows.h)
This is telling you that the function is declared in WinBase.h, but that you should include Windows.h which in turn will include WinBase.h. So, you need to change your include to
#include <Windows.h>
I also wonder why you are talking about mingw considering that your compiler is MSVC. That compiler ships with a comprehensive Windows SDK. Why would you be using an SDK from mingw?

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.

Visual studio compiler errors at enum

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.

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.