<error C2059: syntax error : 'constant'> when compiling with const int - c++

I am getting the following errors when compiling the below code:
3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2059: syntax error : 'constant'
3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2238: unexpected token(s) preceding ';'
#if !defined(AisTarget_h)
#define AisTarget_h
#include "GeneralAviationItems.h"
#include <string>
namespace HEDGE {
using namespace GeneralAviation;
class AisTarget : public WaypointLatLon {
public:
static const int NO_DATA = -1000; //here is the error
};
} // end namespace HEDGE
#endif

It is likely that NO_DATA is already defined as a macro elsewhere, and so it is expanding into something that does not agree with the compiler's notion of a variable name. Try re-naming NO_DATA to something else.
If there were no such conflict, the code as it were would compile fine, as demonstrated here.

Even if this post has its age: The error can generally occur when multiple redefinitions, even regardless of upper/lower case, coexist. This includes potential preprocessor definitions in the solution's .vcprojx file!. Consider something like
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>$(Configuration);%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
in the above mentioned file. Now, having "Debug" and "Release" configurations you will most probably run into some problems and a potential source for the C2059 error. I experienced exaclty this dilemma.

Related

Compile errors when including std::mutex before jemalloc.h

This
//CSocket.h
#ifndef __SERVER_CSOCKET_H__
#define __SERVER_CSOCKET_H__
#include "winsock2.h"
#include "ws2tcpip.h"
#include <thread>
#include <stdio.h>
#include <string>
(cpp includes only the header)
//CSocket.cpp
#include "CSocket.h"
produces the following error messages in c:\program files (x86)\microsoft visual studio 12.0\vc\include\ratio
ratio(122): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(133): See reference to the instance of the just compiled class-template "std::ratio<_Nx,_Dx>".
ratio(124): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(44): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(217): See reference to the instance of the just compiled class-template "std::_Safe_mult<0x01,0x01>".
ratio(36): error C2338: integer arithmetic overflow
ratio(44): See reference to the instance of the just compiled class-template "std::_Safe_multX<0x01,0x01,false>".
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x01,false>'
ratio(44): error C2065: 'value': undeclared identifier
ratio(44): error C2057: Expected constant expression
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x0989680,false>'
ratio(219): error C2975: "_Nx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(116): See declaration of '_Nx'
ratio(219): error C2975: "_Dx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(117): See declaration of '_Dx'
CSocket.cpp
Including std::thread in the .cpp and not in the header solves all errors but I don't know why it doesn't work in the header.
//CSocket.cpp
#include "CSocket.h"
#include <thread>
The only library I am using is jemalloc.
Might the error come from including jemalloc.h before mutex not from thread itself?
I had to #include <mutex> before #include "jemalloc.h" and not afterwards.
Works fine now, strange errors though.
I am having the same error, but the order of the includes is not useful for me. I think this has something to do with other includes that also use chrono and thread, so you can checkout that.
Are you using Visual Studio? Seems like more people is getting the same error: https://connect.microsoft.com/VisualStudio/feedback/details/800726/compiler-error
I have the same errors with VS2013 update 3. The problem seems to be the fact that INTMAX_MAX is not defined, but it is used in ratio.h.
My solution is to add
#define INTMAX_MAX INT64_MAX
before #include <ratio> in your file (if you do not have the line, you may add it).
The line to be included can be found in stdint.h - in your case, the right side can be different.
PS Another solution is to #include <stdint.h> and to define __STDC_LIMIT_MACROS. In this case, you may get some warnings about duplicate macros.
it occurs to the project when i use the CxImage as third party library and with threadpool in C++11. Separately they are all okay, while merged in the same project, that errors occur.
the solution is add the precompile option of _STDC_LIMIT_MACROS to
Property Pages -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions of the Project.
PS: my environment is : MFC/VS2015 && windows7 64bit
may it be helpful to somebody :)

How can i solve this on VC++ 6.0

i've this simple code but i dont where is the error of this all
#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;
int main(){
string s;
cout<<"Entrer nom de fichier avec le source";
cin s;
ifstream fout;
fout.open(s);
s=fout.getche();
fout.close();
cout<<s;
return 0;
}
cause the errors showing after compiling are:
d:\workespace3.cpp(5) : error C2871: 'std' : does not exist or is not a namespace
d:\workespace3.cpp(8) : error C2653: 'std' : is not a class or namespace name
d:\workespace3.cpp(8) : error C2065: 'string' : undeclared identifier
d:\workespace3.cpp(8) : error C2146: syntax error : missing ';' before identifier 's'
d:\workespace3.cpp(8) : error C2065: 's' : undeclared identifier
d:\workespace3.cpp(10) : error C2146: syntax error : missing ';' before identifier 's'
d:\workespace3.cpp(13) : error C2039: 'getche' : is not a member of 'ifstream'
c:\program files (x86)\microsoft visual studio\vc98\include\fstream.h(98) : see declaration of 'ifstream'
Error executing cl.exe.
workespace3.obj - 7 error(s), 0 warning(s)
Don't use the .h forms of the include files, those are meant for backwards compatibility with C. Use for example #include <string>.
You have many errors:
1- You are using deprecated header files. Standard C++ library headers come in headers without ".h". So that would be:
#include <iostream>
#include <fstream>
#include <string>
2- getche() is not a proper method of ifstream. Here is the complete list of methods of ifstream:
http://www.cplusplus.com/reference/fstream/ifstream/
You probably meant to use get() or getline()
3- You are missing "­­>>" between "cin" and "s".
4- You are using a very old IDE. There are multiple newer and free IDEs out there. Notably you can have VC++ 2012 Express for free. It will be more standards compliant and also include better tools and support for C++11
If the compiler supports the C++98 standard headers, then use them
#include <iostream> // no .h
If it only supports the ancient pre-ISO headers that you're including, then just leave out any mention of namespace std. In those dark days, the standard library was just dumped into the global namespace.
I would consider using a compiler from this millennium; you'll find it easier to get help from people whose memory of the 1990s is less than perfect.

Qt QPainterpath.h errors

I am currently working on a Qt application with many #include files from the Qt library. The project is quite large and I have encountered a strange problem when building. The errors displayed are all inside the QPainterpath.h file, but has nothing to do with calls to it from my project. The errors all look like this:
c:\qt\4.8.4\include\qtgui../../src/gui/painting/qpainterpath.h(397) : error C2143: syntax error : missing ';' before '::'
c:\qt\4.8.4\include\qtgui../../src/gui/painting/qpainterpath.h(397) : error C2059: syntax error : '::'
c:\qt\4.8.4\include\qtgui../../src/gui/painting/qpainterpath.h(398) : error C2143: syntax error : missing ';' before '{'
c:\qt\4.8.4\include\qtgui../../src/gui/painting/qpainterpath.h(398) : error C2447: '{' : missing function header (old-style formal list?)
c:\qt\4.8.4\include\qtgui../../src/gui/painting/qpainterpath.h(405) : warning C4003: not enough actual parameters for macro 'elementCount'
I encountered this once before and was able to solve it by moving some Qt #include statements called before other class headers below them. I can't seem to figure out what in particular is causing it this time though. Any thoughts would be great!
Thanks!
I've seen this happen when you forget to close a class definition with a semicolon. If you look at the output from the compiler, and see which file was compiled right before this error started, you may have your answer. Look for syntax errors with missing a ; or an extra } or a missing } at the end of the previously compiled file.
It could also be from a .h file listed above your #include <QPainter> call that has the errors.
Hope that helps.
I'm working with RyGuyFalcore and figured out why this is happening in our code, and nobody else is seeing it. The issue is that our legacy code was redefining the elementCount macro. Therefore, if we included that portion of our legacy code before we included the Qt code, the wrong macro was getting used :( Figured I would post this here to avoid any wild goose chase that others may run into if they hit a similar bug.

c++ boost compilation catastrophe

I'm writing a DLL plugin for a windows application,
after adding the following includes:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
I get a wonderful compilation error:
Error 3 error C2143: syntax error : missing ';' before '__cdecl' c:\program files (x86)\microsoft visual studio 9.0\vc\include\locale.h 111 LeverateMetaTraderServerAPI
Help?
I'm no authority on C++, but this sort of thing happens when you miss a ; off the end of your class definition.
someone who isn't really smart added this :
#define __declspec(dllexport) __stdcall APIENTRY
to one of the API .h files your including
This error is (with high probability) not caused by Boost. It's probably either the result of a missing semicolon somewhere else in your code or triggered by a missing header include leaving some macro undefined.

missing ';' before namespace when using boost 1.4.2

Working on moving some C++ code from Linux over to Windows. The code uses boost 1.4.2, however it keeps failing out on building the boost modules. Basically, every boost hpp file that happens to contain "namespace boost" errors with:
error C2143: syntax error : missing ';' before 'namespace'
Any idea what could be causing this?
Loss of ; before including Boost header could be cause of that. The following code produce such error:
struct X {} // << ; lost here
#include <boost/shared_ptr.hpp>
This small code gives me the following error:
boost/config/suffix.hpp(460) : error C2143: syntax error : missing ';' before 'namespace'
Have you tried including these boost headers on the first line? If they compile fine that way, it's likely a missing ; in one of the headers included before them.