1

Basically what the question says: I'm unable to initialize a vector of any kind in C++;

Here is my code:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {

  //The error occurs under the word "lines" in the following line
  vector<string> lines{"Hello", "there", "General", "Kenobi", "you",
                       "are",   "a",     "bold",    "one"};
}

The error is shown by a red squiggly line under the word "lines" in the program. and here is my error:

no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=std::__1::string, _Allocator=std::__1::allocator<std::__1::string>]" matches the argument list -- argument types are: (const char [6], const char [6], const char [8], const char [7], const char [4], const char [4], const char [2], const char [5], const char [4])C/C++(289)
std::__1::vector<std::__1::string> lines

I looked up this problem extensively and couldn't find a single useful source. One resource I came across advised to use another header file (std_lib_facilities.h) and this didn't work even when I added it. I also tried it with prefixing everything with std:: and not using std as the namespace, but this also didn't do anything.

As a last ditch, I copied working examples of code from online and tried to run them, and I got the same, reproducible error each time, even when creating vectors of ints or chars.

I'm sure I'm missing something simple, but I have been unable to figure it out. If you know, please help me out.

As always, if you answer or attempt to answer this question, thank you for your time.

EDIT: The question that this was suggested was a duplicate of is not. The solution to this problem involved editing my tasks.json file and the other answer had essentially nothing to do with my issue.

Anish Sinha
  • 175
  • 3
  • 13
  • 4
    Are you compiling the above using C++11 or above? List-initializers don't work pre-C++11. – Ruks Aug 18 '21 at 01:28
  • 1
    This is probably what Ruks mean `g++ -std=c++11 test.cpp` – solti Aug 18 '21 at 01:31
  • @Ruks I'm not sure, how do I check? My tasks.json looks like: ```{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: clang++ build active file", "command": "/usr/bin/clang++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "compiler: /usr/bin/clang++" } ] }``` – Anish Sinha Aug 18 '21 at 01:36
  • @Ruks This pretty much worked! The red squiggly line didn't go away, but the program compiled and ran correctly. It still says that there's no constructor with an error message, but the program itself works. Thanks for the advice – Anish Sinha Aug 18 '21 at 01:48
  • Does this answer your question? [Where can we use list initialization?](https://stackoverflow.com/questions/13267277/where-can-we-use-list-initialization) – prehistoricpenguin Aug 18 '21 at 02:19
  • @Ruks spot on, solved my problem. If you copy/paste your comments as an answer, I'll accept it. Worked like a charm – Anish Sinha Aug 18 '21 at 02:24
  • @prehistoricpenguin No, not at all. That answer lists some facts about list initialization, and would not have helped me had I seen that answer prior to asking this question, whose answer was modifying the tasks.json file to include another parameter in args[]. It wasn't a matter of not knowing in which version list initialization could be used, but how to configure the compiler to work with the right version. – Anish Sinha Aug 18 '21 at 02:39

1 Answers1

4

To compile your code using C++11, change your tasks.json file to this:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++11",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "compiler: /usr/bin/clang++"
    }
  ]
}

This will properly compile your code with C++11 using clang++.


The error is shown by a red squiggly line under the word "lines" in the program.

To get rid of the "red squiggly lines", you need to get IntelliSense to work properly in VSCode. For that, you need to configure the extension that you are using to use C++11 as well.

If you are using Microsoft's C/C++ extension, you need to go to: File > Preferences > Settings from the menu, then enter C/C++ in the search bar, look for a setting named Cpp Standard, and in the drop-down list of various C++ standards, select C++11.

VSCode Setting Image

Ruks
  • 3,886
  • 1
  • 10
  • 22