I get 3 errors :
I don't understand why, my code seems to be correct. I've tried with a 1D vector but I get the same error. For me, my syntaxe is correct. For the operator's issue, I've seen plenty of topics about it but no one could help me in my case.
I'm on Mac using Clang with C++20 in VS Code. I've checked my settings, my configurations etc, all seems to be up-to-date and correct.
Can someone explain me what's the problem here please ?
#include <iostream>
#include <vector>
std::vector < std::vector <int> > interchange(std::vector< std::vector<int> > vect)
{
/* In : a 2d vector containing only binaries.
Out : the previous 2d vector with the binaries interchanged. */
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
if (vect[i][j] == 0)
{
vect[i][j] = 1;
}
else
{
vect[i][j] = 0;
}
}
}
return vect;
}
int main()
{
std::vector < std::vector <int> > vect
{
{1, 0, 1},
{1, 1, 1},
{0, 0, 1}
};
std::vector < std::vector <int> > a = interchange(vect);
std::cout << a << std::endl; // making sure that my function works
std::cout << "Hello World!\n" << "This is my program!\n"; // making sure that the program runs
}
.vscode configuration files :
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20"
}
],
"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": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++2a",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Well, I found the solution.
For the "expected ';' at end of declaration [29, 43]" error:
The VS Code IntelliSense is showing an error, but because the compiler does not found any error, my code runs. As a solution, we can ignore it, while it does not block the compiling process. Even sometimes, the error does not appear... It may be a bug from VS Code, as my JSON files are setup correctly and everything up-to-date.
For the invalids operands errors:
That's because we can't "cout" a vector apparently. So in order to print a vector, we have to create a function for it.
Here are my files, everything works. For the JSON files, you can copy mines, but you will problably just have to change some paths:
main.cpp:
#include <iostream>
#include <vector>
void invert(std::vector< std::vector <int> > vector)
{
/* In : a 2d vector containing only binaries.
Out : the previous 2d vector with the binaries interchanged. */
for (int i = 0; i < vector.size(); i++)
{
for (int j = 0; j < vector[i].size(); j++)
{
if (vector[i][j] == 0) vector[i][j] = 1;
else vector[i][j] = 0;
std::cout << vector[i][j] << " ";
}
std::cout << std::endl;
}
}
void print2DVector(std::vector< std::vector <int> > vector)
{
for (int i = 0; i < vector.size(); i++)
{
for (int j = 0; j < vector[i].size(); j++)
{
std::cout << vector[i][j] << " ";
}
std::cout << std::endl;
}
}
int main()
{
std::vector < std::vector <int> > vector
{
{1, 0, 0, 0, 1},
{1, 1, 0, 1, 1},
{0, 1, 0, 1, 0}
};
print2DVector(vector);
std::cout << "\nInverting...\n\n";
invert(vector);
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20"
}
],
"version": 4
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++2a",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Terminal:
1 0 0 0 1
1 1 0 1 1
0 1 0 1 0
Inverting...
0 1 1 1 0
0 0 1 0 0
1 0 1 0 1
Good luck on your coding projects!