Compiler Error on using Braced Initialisation C++ - c++

The following error is received while using empty braces to initialise variables:
error: expected ';' at end of declaration
int b{};
^
;
The code being used is:
#include<iostream>
int main(){
int a{};
std::cout<<"hello"<<std::endl;
return 0;
}
I'm using the latest version of Microsoft VS Code on an M1 based Macbook Air. Extensions being used are C/C++ Intellisense and Code Runner.
Same error is displayed when running the file in terminal.
The tasks.json file looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
int a={4}; works fine while int a={}; throws an error error: scalar initializer cannot be empty
As a person who is habituated of using braced initialisation, this issue is giving me quite some trouble. Any fix for this issue would be appreciated.

Related

Declaring and initializing a vector in C++ 17

This code doesn't compile in VS Code for Mac.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using std::vector;
using std::cout;
int main()
{
std::vector<int> v{23, 52, 9};
for (int const& i : v)
{
std::cout << "Hello " << i << "!\n";
}
}
Error:
hello.cpp:11:23: error: expected ';' at end of declaration
std::vector<int> v{23, 52, 9};
My c_cpp_properties.json suggests I'm using C++ 17.
This is the output of tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/clang++"
}
],
"version": "2.0.0"
}
It is expecting me to not supply initial members of the array/list. Really weird. Same code works if I use the same declaration without initializing it, but using v.push_back().

C++ requires keyword does not work, compiler error [duplicate]

This question already has answers here:
Visual Studio Code: how to add arguments for g++ compiler?
(2 answers)
Closed 10 months ago.
Code is supposed to work but it gives compiler error:
#include <iostream>
#include <concepts>
using namespace std;
template <typename T> requires integral<T>
T add( T a, T b){
return a + b;
}
int main(){
char a_0{10};
char a_1{20};
auto result_a = add(a_0,a_1);
cout << "result_a : " << static_cast<int>(result_a) << endl;
int b_0{11};
int b_1{5};
auto result_b = add(b_0,b_1);
cout << "result_b : " << result_b << endl;
return 0;
}
Compiler error:
error: 'requires' does not name a type;
6 | requires integral
Im using vs code and mingw64 from this site: https://winlibs.com
Here is my vscode task.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\mingw64\\bin\\g++.exe"
}
]
}
requires is a keyword available since C++20.
To compile this code using GCC, specify -std=c++20 as a command-line argument.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-std=c++20",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\mingw64\\bin\\g++.exe"
}
]
}

C++ Uniform initialization doesn't work with "expected ';' at end of declaration"

I'm the beginner of C++. I set development environment in vscode in Mac.
But I trapped in variable initialization.
Copy initialization and direct initialization are ok, but uniform initialization doesn't work.
#include <iostream>
int main() {
int a(0);
int b = 0;
int c{0};
int d = {0};
int e;
std::cout << "Hello World";
return 0;
}
In this code, int c{0}; doesn't work. It failed to build.
It printed error: expected ';' at end of declaration
What can I try to solve this problem??
I don't know if it helps, but I'll upload ".vscode".
// c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
// launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"&&",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ 활성 파일 빌드",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "디버거에서 생성된 작업입니다."
}
]
}

Set breakpoints in C++ debugger (vs code)

The issue I'm having is that when I run "C++ run/debug active file" the the code executes but doesn't stop at the breakpoints I set. I don't know a lot about how debuggers work but I'm guessing the the debugger isn't listening to the correct process? Or why would this happen?
In all honesty, I've been struggling to get all my dependencies correct and in the process may have broke intended functionality so it could be that I set up the scripts in a way that breaks it's intended use.
tasks.json looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "g++",
"args": [
"main.cc",
"-o",
"main",
"-std=c++17",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
launch.json looks like this:
{
"version": "0.0.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
c_cpp_properties.json looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "g++",
"args": [
"main.cc",
"-o",
"main",
"-std=c++17",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
I reworked it and went slower this time. I don't have a launch file but
Here's tasks:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
And here's c_cpp_properties
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/lib",
"/usr/local/lib/cairomm-1.16/include",
"/usr/local/include/cairomm-1.16",
"/usr/include/sigc++-3.0",
"/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"/usr/include/cairo",
"/usr/include/freetype2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++20",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
I think there are a lot of unnecessary code in your tasks.json , try this out I am successfully able to set breakpoints during debugging .
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}

vector<int> cannot be initialized with an initializer list in Visual Studio Code

I am using Visual Studio Code on Mac OS X to build and run a very basic vector program. Here is
the code
#include <iostream>
#include <vector>
using namespace std;
int main(){
// Demo vector
vector<int> arr = { 1,2,3,4,55};
cout<<arr.size()<<endl;
return 0;
}
The below code on running gives following error
vectors.cpp:8:17: error: non-aggregate type 'vector' cannot be
initialized with an initializer list
vector arr = { 1,2,3,4,55};
^ ~~~~~~~~~~~~~ 1 error generated.
I also added "-std=c++11", in tasks.json, restarted and visual studio code, but the error remains the same. Here is the tasks.json for reference
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"label": "tsc: build - tsconfig.json"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build",
"label": "tsc: watch - tsconfig.json"
},
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang"
}
]
}
this is the command being built by Visual Studio Code
cd "/Users/XXX/PROJECTS/Algorithms/" && g++ vectors.cpp -o vectors &&
"/Users/XXX/PROJECTS/Algorithms/"vectors
Can some one suggest a way to run this program within Visual Studio Code editor?
Thanks!
I still could not use the Visual studio code run button to run the program, so I ended up running via command prompt using below command
$ g++ -std=c++11 -o test test.cpp
If someone can share their task.json, it will be good to see. Here is mine
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g++",
"--std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}