I'm using C++. I tried to declare a variable like this:
int my_num {5};
I got an error in the logs:
"error: expected ';' at end of declaration"
I get the same error when I do this:
int my_arr [] {1,2,3,4};
I am new to C++ (normally work with JS). Any idea what's going on here?
c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Related
I'm trying to compile the following C++ code on Visual Studio Code, using the Mac clang compiler.
#include <iostream>
int main() {
int x { 5 };
std::cout << x;
return 0;
}
However, this returns an error, on the line of the list initialization: int x{ 5 };. Specifically, it says I need to insert a semicolon after the x.
I don't get what's wrong with this code, it works fine on an online compiler. How do I fix this?
Running man clang in the Terminal and skimming through, I found this:
The default C++ language standard is gnu++14.
UPDATE: I ran clang++ main.cpp in the compiler and it returned that semicolon error. This isn't a problem with VSCode, so I'll remove that tag.
Here's the error:
main.cpp:3:10: error: expected ';' at end of declaration
int x { 5 };
^
;
1 error generated.
Having such a super simple char array definition:
const int g_msgbuf_size = 200;
char g_msgbuf[g_msgbuf_size];
giving me an totally unreasonable error:
error C2057: expected constant expression
on the second line while compiling in MSVC 2019.
When i try the code on any other c++ compiler like https://www.onlinegdb.com/online_c++_compiler or http://cpp.sh/ everything goes well - I get no errors as expected.
Why does the MSVC 2019 makes me problems?
Consider an enum class
enum class FOO
{
A,B,C
};
struct something
{
FOO abc = FOO::A; //Compiler Doesnt like this
}
int main(){
something _something;
return 0;
}
So the compiler doesnt like the initialisation and gives me 3 different errors.
Error C2238 unexpected token(s) preceding ';'
Error C2059 syntax error: '='
Error C2653 'FOO': is not a class or namespace name
Compiler:
Visual C++
Build Tools MsVC142
So am I initialisation the enum incorrectly? or am i completely missing something.
Mac g++ throws a compilation error with this code:
#include <iostream>
using namespace std;
int main(int argc, char ** argv) {
int * p = new int[5] {1,2,3};
return 0;
}
I tried an online compiler and it compiles and runs without errors.
Is there something wrong with mac compiler? Can I do something to change how it works or should I install another c++ compiler?
Edit:
The error:
test.cpp:6:25: error: expected ';' at end of declaration
int * p = new int[5] {1,2,3};
Compiler target and version:
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Compile command:
g++ test.cpp
There's no fundamental reason not to allow a more complicated initializer it's just that C++03 didn't have a grammar construct for it. In the next version of C++, you will be able to do something like this.
int* p = new int[5] {0, 1, 2, 3, 4};
You can try adding -std=c++11 to your command line and that should be working all fine.
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);