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"
}
]
}
Related
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().
This is my program:
#include<iostream>
#include<D:\installation folder\opencv\build\include\opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image;//**LOOK HERE**---(1)
// if ( !image.data )
// {
// printf("No image data \n");
// return -1;
// }
// namedWindow("Display Image", WINDOW_AUTOSIZE );
// imshow("Display Image", image);
// waitKey(0);
cout << "executed" << endl;
return 0;
}
Whenever I comment line-(1), program is compiled and ran successfully. But whenever I uncomment any opencv-code inside main() program is not compiled and vsCode shows error like
main.exe doesn't exists.
I don't know where is the problem. I am new in openCV.
I am using vsCode and gcc-compiler.
If you need more info, let me know in comment
Extra:task.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "D:\\installation folder\\mingw64\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\installation folder\\OpenCV-MinGW-Build-OpenCV-4.5.5-x64\\include",
"-L",
"D:\\installation folder\\OpenCV-MinGW-Build-OpenCV-4.5.5-x64\\x64\\mingw\\bin",
"-llibopencv_calib3d411",
"-llibopencv_core411",
"-llibopencv_dnn411",
"-llibopencv_features2d411",
"-llibopencv_flann411",
"-llibopencv_highgui411",
"-llibopencv_imgcodecs411",
"-llibopencv_imgproc411",
"-llibopencv_ml411",
"-llibopencv_objdetect411",
"-llibopencv_photo411",
"-llibopencv_stitching411",
"-llibopencv_video411",
"-llibopencv_videoio411"
],
"options": {
"cwd": "D:\\installation folder\\mingw64\\x86_64-w64-mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\installation folder\\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": "Task generated by Debugger."
}
]
}
Terminal:
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.
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++"
}
]
}
I am trying to learn c++17 with Visual Studio Code on Ubuntu 20.04 with the following tasks.json
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++2a",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
And I have the following code, which compiles and runs well.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> mSet;
auto [iter, inserted] = mSet.insert(10);
return 0;
}
But the syntax checker says that there is 3 problems.
I don't think that there is any problem with my code, so how can I make Visual Studio Code syntax checker work properly?