I am getting the following errors when compiling the below code:
3>c:hedgehedgehedgeAisTarget.h(22) : error C2059: syntax error : 'constant'
3>c:hedgehedgehedgeAisTarget.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
asked Aug 2, 2012 at 16:41
3
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.
answered Aug 2, 2012 at 16:45
jxhjxh
68.8k8 gold badges110 silver badges191 bronze badges
3
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.
answered Apr 21, 2016 at 7:32
gilgamashgilgamash
86210 silver badges31 bronze badges
1
description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Learn more about: Compiler Error C2059 |
Compiler Error C2059 |
03/26/2019 |
C2059 |
C2059 |
2be4eb39-3f37-4b32-8e8d-75835e07c78a |
Compiler Error C2059
syntax error : ‘token’
The token caused a syntax error.
The following example generates an error message for the line that declares j
.
// C2059e.cpp // compile with: /c // C2143 expected // Error caused by the incorrect use of '*'. int j*; // C2059
To determine the cause of the error, examine not only the line that’s listed in the error message, but also the lines above it. If examining the lines yields no clue about the problem, try commenting out the line that’s listed in the error message and perhaps several lines above it.
If the error message occurs on a symbol that immediately follows a typedef
variable, make sure that the variable is defined in the source code.
C2059 is raised when a preprocessor symbol name is re-used as an identifier. In the following example, the compiler sees DIGITS.ONE
as the number 1, which is not valid as an enum element name:
#define ONE 1 enum class DIGITS { ZERO, ONE // error C2059 };
You may get C2059 if a symbol evaluates to nothing, as can occur when /Dsymbol= is used to compile.
// C2059a.cpp // compile with: /DTEST= #include <stdio.h> int main() { #ifdef TEST printf_s("nTEST defined %d", TEST); // C2059 #else printf_s("nTEST not defined"); #endif }
Another case in which C2059 can occur is when you compile an application that specifies a structure in the default arguments for a function. The default value for an argument must be an expression. An initializer list—for example, one that used to initialize a structure—is not an expression. To resolve this problem, define a constructor to perform the required initialization.
The following example generates C2059:
// C2059b.cpp // compile with: /c struct ag_type { int a; float b; // Uncomment the following line to resolve. // ag_type(int aa, float bb) : a(aa), b(bb) {} }; void func(ag_type arg = {5, 7.0}); // C2059 void func(ag_type arg = ag_type(5, 7.0)); // OK
C2059 can occur for an ill-formed cast.
The following sample generates C2059:
// C2059c.cpp // compile with: /clr using namespace System; ref class From {}; ref class To : public From {}; int main() { From^ refbase = gcnew To(); To^ refTo = safe_cast<To^>(From^); // C2059 To^ refTo2 = safe_cast<To^>(refbase); // OK }
C2059 can also occur if you attempt to create a namespace name that contains a period.
The following sample generates C2059:
// C2059d.cpp // compile with: /c namespace A.B {} // C2059 // OK namespace A { namespace B {} }
C2059 can occur when an operator that can qualify a name (::
, ->
, and .
) must be followed by the keyword template
, as shown in this example:
template <typename T> struct Allocator { template <typename U> struct Rebind { typedef Allocator<U> Other; }; }; template <typename X, typename AY> struct Container { typedef typename AY::Rebind<X>::Other AX; // error C2059 };
By default, C++ assumes that AY::Rebind
isn’t a template; therefore, the following <
is interpreted as a less-than sign. You must tell the compiler explicitly that Rebind
is a template so that it can correctly parse the angle bracket. To correct this error, use the template
keyword on the dependent type’s name, as shown here:
template <typename T> struct Allocator { template <typename U> struct Rebind { typedef Allocator<U> Other; }; }; template <typename X, typename AY> struct Container { typedef typename AY::template Rebind<X>::Other AX; // correct };
- Remove From My Forums
-
Question
-
error C2059: syntax error : 'constant' HI i have a file named SharedCount.hxx. i this file i have a class that ovveride operator new. problem with this is when i want to use this class compiler generate this error error C2059: syntax error : 'constant' and error line point to operator new overloading. exactly this file is modified version of boost shared_ptr.i use these file in another projects without any problem(vs c++ 2010). please help me to solve this error. unfortunately original code is very very long to be posted here but if it is required i will post it compeletly *******************************************SharedCount.hxx Snippet****************************************************
#include "stdafx.h" #include <memory> // std::auto_ptr, std::allocator #include <functional> // std::less #include <exception> // std::exception #include <new> // std::bad_alloc #include <typeinfo> // std::type_info in get_deleter #include <cstddef> // std::size_t #include "ILock.hxx" #include "ILMutex.hxx" template<class P, class D> class ICORE_API sp_counted_base_impl: public sp_counted_base { private: P ptr; // copy constructor must not throw D del; // copy constructor must not throw sp_counted_base_impl(sp_counted_base_impl const &); sp_counted_base_impl & operator= (sp_counted_base_impl const &); typedef sp_counted_base_impl<P, D> this_type; public: // pre: initial_use_count <= initial_weak_count, d(p) must not throw sp_counted_base_impl(P p, D d): ptr(p), del(d) { } virtual void dispose() // nothrow { del(ptr); } virtual void * get_deleter(std::type_info const & ti) { return ti == typeid(D)? &del: 0; } void * operator new(size_t) { return std::allocator<this_type>().allocate(1, static_cast<this_type *>(0)); } void operator delete(void * p) { std::allocator<this_type>().deallocate(static_cast<this_type *>(p), 1); } };
#WebCan Video Conference System Based ON SL#
Answers
-
Then it must be some other file (not yours) where the ‘new’ is redefined.
No, the error is related to ‘#define new …’ only.
You could:
1) Seach for ‘#define new’ in files that are included. To trace the included files,
set «Show Includes» setting in C/C++ properties of your project. When building, the Output Windows will contain all that files. Find your error in the ‘Output Window’ and search the related ‘included’ files
for ‘#define new’.OR
2) Exclude some files from building and comment-out portions of your code until the problem disappears
OR
3) Create a new project and reproduce the problem with minimum amount of code. Next, use steps (1) and (2) to find the problem.
OR
4) Place
#undef new
to the top of your file. But I’m not sure it will work as the root cause of that problem is still there.
-
Marked as answer by
Monday, July 16, 2012 8:53 AM
-
Marked as answer by
-
Amir110 wrote:
i test it and error trigers during bulding.
but i never override operator new in a whole of project except in SharedCount.hxx.
Put this line near the beginning of SharedCount.hxx:
#define new ANYTHING
You would get an error about macro redifinition. The error message would point to the spot of the previous definition. This way, you’ll find out where new is defined as a macro.
Igor Tandetnik
-
Proposed as answer by
Grigoriy Chudnov
Sunday, July 15, 2012 7:34 PM -
Marked as answer by
Amir110
Monday, July 16, 2012 8:53 AM
-
Proposed as answer by
I want to read and print the first two lines from a text file.
Problem is, I get the error: error c2059: syntax error: constant and it points to the first line in my text file.
Any ideas?
The file.txt:
5
5
3
1 1 1 0 0
0 1 0 0 1
0 1 0 1 0
1 0 1 0 1
1 1 0 1 1
The code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
int line, col, gen;
fp = fopen("file.txt", "rt");
fscanf(fp, "%dn,%dn", &line, &col);
printf("line: %d, col: %dn", line, col);
fclose(fp);
return 0;
}
asked Dec 11, 2014 at 10:42
8
Visual Studio will compile every file in your project. This includes file.txt
, if you have added it as a file to your project.
To prevent Visual Studio from compiling this, you need to tell Visual Studio it’s a ‘Content’ file. Take a look at File Properties at Build Action Property.
Content — The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
answered Dec 11, 2014 at 11:02
bzeamanbzeaman
1,12811 silver badges28 bronze badges
1
Solution 1
true
and false
are keywords representing constant values in C++. You cannot use them to name things such as enum values.
As an example, the following would compile
enum { false_, true_ };
int main() {}
Solution 2
false
and true
are reserve words in C++. You can’t redefine it as variable.
Related videos on Youtube
01 : 21
Syntax error, unexpected end of file in PHP First and Easy Solution.
08 : 24
How to Handle Syntax Errors in Python? Python Syntax Errors
04 : 01
Parse error syntax error, unexpected T VARIABLE in PHP:(Fixed)
07 : 07
01 : 30
Parse error: syntax error, unexpected } PHP Solved
01 : 48
PHP HOW TO FIND: «Parse error: syntax error, unexpected end of file in…»
06 : 25
How to Fix Any Syntax Error in Unity (CS1003, CS1525, CS1026, etc)
Unity Game Programming For Beginners
16 : 11
Basic Errors in Python (Solved) | #python, #BugSolver
02 : 55
Two ways to Solve error «syntax error in insert into statement vb.net access database»
Programming for Everybody
02 : 11
Parse error syntax error, unexpected ‘else’ (T_ELSE)
Computer Programming Tutor
02 : 28
Syntax error, ‘for each’ statements are only available if source level is 1.5 or greater
04 : 00
FatalErrorException in Inflector.php line 265: syntax error, unexpected ‘:’, expecting ‘;’ or ‘{‘
01 : 49
How to Fix unable to parse the subtitle file syntax error at line
15 : 01
How do you fix a syntax error in Assembly Programming Part-1
Comments
-
I have the following code in a header file:
enum {false,true};
and I have my main function in main.c. if I change the extention to main.cpp
I get the following error:Error C2059: syntax error 'constant'
Im using visual c++, any Idea why`?
-
You’ve found one of the reasons why C++ cannot be made 100% backwards compatible with C.
-
-
It is also true for other constants (enumeration for instance) that are already defined elsewhere (I encountered a problem when I have defined an enumeration value named NO_ERROR which is already defined defined in winerror.h).