Hello everyone, I’m trying to start using VSCode to code. I have a couple concerns with the app:
When I type a for loop like so:
for(int i=0;i<n;++i) {
When I try to press enter after the first bracket, it doesn’t automatically indent for me and make a new line like this ("|" being where my typing thing is)
for(int i=0;i<n;++i) {
|
}
and instead looks like this
for(int i=0;i<n;++i) {
|}
This is a very unpleasant issue I didn’t have to deal with in online IDEs.
When I make a 2D vector like such:
vector<vector> v;
I get a compile error saying I need to put a space in between the last 2 ">"s like this:
vector<vector > v;
While this is a minor inconvenience, I don’t really like it and would prefer knowing how to fix it.
Auto formatter
When right-clicking a place in my code, there is an option to automatically format my code (a beautifier).
This is what my code originally looked like:
This is what my code looked like after formatting:
While I want it to look like
Is there any similar preset I can somehow use to do this? And how do I set the preset?
You’ll need a .clang-format file in your project directory: see some of the links from Adding Solutions. Alternatively, you can search “formatting” in settings and set “C_Cpp: Clang_format_fallback Style.”
Hey Benq,
The solution to my third problem worked. However, after changing the C standard and C++ Standard to c17 and c++17, it still gives me the error for the vector initialization
error: a space is required between consecutive right angle brackets (use '> >')
vector<vector<int>> v;
^~
> >
I reset my settings and modified some that I think were very important, and most my problems were solved. I still need the solution to the 2d vector problem though.
The screenshot you sent is a little different from the one in my response; did you try going to “C/C++: Edit Configurations (UI)” and editing the C++ standard there?
I tried changing the C++ standard to c++03 and nothing changed.
Also, I was using GCC and changed to clang++, but it didn’t change anything either.
I tried putting -std=c++17 in the compiler arguments and nothing changed either.
Do you see anything happen at all when you change the compiler arguments or C++ standard? For example, if you switch between c++98 and c++03, you should see the list of problems disappear briefly and then reappear. Same for the compiler arguments.
(Note: for the compiler arguments, you need to click outside of the text box after typing them in for them to take effect.)
When I run your code with c++17 and c++03, here are all the errors (all the same):
test.cpp:6:19: error: a space is required between consecutive right angle brackets (use '> >')
vector<vector<int>> v;
^~
> >
test.cpp:8:2: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto [l, r] = p;
^
test.cpp:8:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
auto [l, r] = p;
^~~~~~
test.cpp:8:8: error: binding reference of type 'std::__1::tuple_element<0, std::__1::pair<int, int> >::type' (aka 'int') to value of type 'const typename tuple_element<0UL, pair<int, int> >::type' (aka 'const int') drops 'const' qualifier
auto [l, r] = p;
^
test.cpp:8:8: note: in implicit initialization of binding declaration 'l'
2 warnings and 2 errors generated.
Also I have another problem. When I try to auto-format, it doesn’t work, saying “The ‘clang-format’ command is not available. Please check your clang-format.executable user setting and ensure it is installed.”
I’m not sure how to fix this.
By “run my code” do you mean you are executing g++ -std=c++03 test.cpp from the terminal? That produces the errors you showed above, but g++ -std=c++17 test.cpp shouldn’t produce any.
When I say run, I am clicking the play button at the top right corner. When I tried using your command g++ -std=c++17 test.cpp it worked. But I don’t really want to type this command every time I want to run my code, is there another way to run my code or make this my default running command?