C++ Expected ; at end of declaration

When using braced initialisation in C++, you may encounter the error expected ‘;’ at end of declaration. An example image of this from the VS Code terminal is shown below:

expected semicolon c++ error

If you see this or any other error that you expect is valid C++, you need to check the compiler version to see what C++ version you are compiling to.

I used Clang 14.0 on macOS inside VS Code in the case above. The fix to resolve the bug was to set the C++ STD version in the compiler command line flags.

In VS Code, these flags are set in the task settings. To access your task settings, go to the .vscode folder and open tasks.json

tasks json file

In the tasks array “tasks”: [ you will see the args array. In this array, we can add a flag for the compiler to specify the C++ version. 

In my case, I was able to add support for C++ 11 by adding the following arg: ”-std=c++11 as shown in the next image:

Fixed Tasks JSON file

Running the build tasks after recompiling shows no more errors. 

Successful Build Result

In conclusion, if you expect that your code is correct and that IntelliSense or a linter is providing erroneous error messages, double-check your individual project environment to ensure that you are compiling against the correct C++ standard.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments