Error C2059: syntax error 'constant' [duplicate] - c++

This question already has answers here:
Why am I getting "error: expected '}'" in C++ but not in C?
(3 answers)
Closed 9 years ago.
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`?

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() {}

false and true are reserve words in C++. You can't redefine it as variable.

Related

C syntax - error C2143: syntax error : missing ')' before '*' [duplicate]

This question already has answers here:
Is bool a native C type?
(12 answers)
Closed 6 years ago.
Why am I getting a syntax error for my C header declaration?
Here is my header file, viterbi.h:
#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int , bool* );
#endif //VITERBI_H
And here is my implementation file, viterbi.c:
// viterbi.c : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate, bool* hd);
void vitdec(float* sd, int frameLen, int rate, bool* hd)
{
//... The rest of the function
The errors from the Visual Studio 2010 compiler read:
viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect
As far as I have seen/can tell, this is valid syntax for a C declaration. If I compile viterbi.c as C++ code (viterbi.cpp), then the errors disappear. What is the syntax error?
bool is not a native C type, but for those using C99, try adding the line #include <stdbool.h>, which contains a macro that defines bool.
Since the C compiler in all Visual Studio/MSVC products uses C89, bool is not defined at all for you, as a native C type or otherwise. Workarounds include using typedef or enum to define bool. Examples are in the below link.
For more information, see: Is bool a native C type?

why type casting on non-pointer struct give syntax error

I am using Visual C++ express 2008 try to compile code similar to below:
no problem
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008
It seem like the same code will be compiled with no error/warning on gcc compiler. Why I get this error on VC++? Is it a C/C++ standard way to do casting on struct value? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
Use av_get_time_base_q() instead of AV_TIME_BASE_Q for C++ or VS.
This was fixed in a patch
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options:
Rename your program with a .c suffix
Switch on the /TC flag for the program that does not compile.
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.
For compound-literals errors in C++
wrong:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);

C++ constexpr error statement showing error [duplicate]

This question already has answers here:
constexpr not compiling in VC2013
(4 answers)
Closed 9 years ago.
Please help me with this error.
I am trying to make a sample that explains constexpr keyword in c++. I am using Visual Studio 2013.
Following is the code of my cpp file.
#include <iostream>
#include <stdexcept>
const int sampleconstant = 5;
constexpr int constTest(void)
{
return sampleconstant;
}
int main()
{
std::cout << constTest();
getchar();
return 0;
}
This shows compile time error as follows:
Error 1 error C2144: syntax error : 'int' should be preceded by ';'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I may be doing something silly here. But really can't figure out this error. Code looks ok to me. If any one can help me with error please help.
As already told you, it is not supported on VS.
You can see at this link a list of featured supported by the compilers: http://wiki.apache.org/stdcxx/C++0xCompilerSupport

error C2061 while passing vector as an argument [duplicate]

This question already has answers here:
C++: Syntax error C2061: Unexpected identifier
(5 answers)
Closed 9 years ago.
I'm getting this error:
1>b:\projects\c++\wolvesisland\wolvesisland\wolvesisland\board.h(22): error C2061: syntax error : identifier 'vector'
while trying to pass vector as an argument.
(it's about function move())
Code:
Board.h
#pragma once
#include <vector>
enum field_state {is_wolfm, is_wolff, is_rabbit, is_bush, is_nobody};
struct state{
field_state field;
int number;
};
class Board
{
private:
state island[20][20];
public:
Board(void);
~Board(void);
void fill(int,int,int,int, vector<LivingForm*>*);
state get_island(int,int);
void set_state(int,int,field_state,int);
};
and the function declaration in Board.cpp:
void Board::fill(int rabbit,int wolfm,int wolff,int bush,vector <LivingForm*> *creatures)
vector lives in the std namespace, so you need to refer to ut as std::vector:
void fill(int,int,int,int, std::vector<LivingForm*>*);
// ^^^

Syntax errors when passing string argument onto function

Visual Studio (2012/C++) is reporting multiple errors all relaxing to errors in syntax when I cannot see any errors.
I'm calling the function with Ping(ID); and ID is a string (Already defined), I've defined the function in the relevant header file as
#include <string>
int Ping(string ID);.
A stripped down version of the function is
int Ping(string ID)
{
// Ping
cout<<"Pinging\n";
cout<<ID;
return (1);
}
and the errors in the header file are as follows;
Error 3 error C2059: syntax error : ')' func.h 3 1
Error 1 error C2065: 'string' : undeclared identifier func.h 3 1
Error 2 error C2146: syntax error : missing ')' before identifier 'ID' func.h 3 1
I'm really stuck with what I need to do to fix this, so any guidance on how to fix it will be greatly appreciated.
You did not qualify the name with std::, as std::string.