Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Code Need Help - no instance of constructor matches argument list

I am following a book on C++ programming and I got stuck on vectors. The example from the book goes:

vector<int> v = {1,2,3};

but I'm getting an error:

    1   IntelliSense: no instance of constructor "Vector<T>::Vector [with T=int]" matches the argument list
        argument types are: (int, int, int) ../path

Also, when I create string vector:

vector<string> v = {"one", "two", "three"}

I get this error:

    1   IntelliSense: no instance of constructor "Vector<T>::Vector [with T=std::string]" matches the argument list
        argument types are: (const char [4], const char [4], const char [6]) ../path

I am using VS 2013 with Nov 2013 CTP compiler. What am I doing wrong?

like image 794
user3650284 Avatar asked Aug 09 '14 06:08

user3650284


2 Answers

To summarize and expand upon what was written in the comments and Bjarne Stroustrup's "std_lib_facilities.h" header:

  • The header contains a trivially range-checked vector class called Vector for teaching purposes;
  • To make Vector a "seamless" replacement for vector in the standard library (again, for teaching purposes), the header contains the following lines:

    // disgusting macro hack to get a range checked vector:
    #define vector Vector
    
  • The OP likely used the header for the first edition of the book (it's the top Google search result for std_lib_facilities.h), whose Vector doesn't have an initializer_list constructor (that edition uses C++98, which doesn't have initializer lists).
  • As a result, the compiler complains that Vector doesn't have a matching constructor when it sees vector<int> v = {1,2,3};, which becomes Vector<int> v = {1,2,3}; after macro replacement.

To fix the problem, download and use the correct version of the header.

like image 140
T.C. Avatar answered Sep 25 '22 06:09

T.C.


This may be related to the compilation environment you have configured.

Take mine as an example:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
     vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

     for (const string& word : msg) {
          cout << word << " ";
     }
     cout << endl;
}

If that comiplers in online compiler, that successfully run.

eg:programiz:

https://www.programiz.com/cpp-programming/online-compiler/

Also, that output:

Hello C++ World from VS Code and the C++ extension! 

But if that compilers in Visual studio Code, that will have the same problem as you.

“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”

How did that happen? Or, what did happen?

I never do not configure these files, eg:"launch.json","c_cpp_properties.json".

I compiled that directly after downloading VS Code and plug-ins C/C++ directly.

I think the trick to solve the problem is to make sure that there is no problem with the configured compilation environment.

For example: If there is a problem in the IDE, then I can migrate it to an online compiler to test whether it can be compiled successfully.

If you are like me, and the same problem occurs without the configuration file, then the root cause of the problem is the failure to configure the file.

Finally, it's difficult for Visual Studio Code to configure C/C++ compilation environment files in macOS, but it can be solved.

What's more, maybe that is connected to your C++ Language version,

Use g++ -std=c++11 <filename> when compiling.

It's better to use this command to run your code:

g++ -g -Wall -std=c++11 helloworld.cpp

Or, please configure your tasks.json in VS code:

"args": [
      "-g",
      "-Wall",
      "-std=c++11",
      "-stdlib=libc++",
      "-g",
      "${file}",
      "-o",
      "${fileDirname}/${fileBasenameNoExtension}"
    ],

add "-g", "-Wall" in the "args" of the tasks. In short,

firstly,

this error is reported because C++98 does not allow you to specify the initial element value when initializing the vector container.

That was in C++11, you need to configure c++11 support.

  1. If your problem was in VS code, then configure c++11 support in .vscode/task.json, plus -std=c++11, -stdlib=libc++ configuration.

Secondly,

If you also use the C/C++ Clang Command Adapter plug-in in the VS code,

because of C/C++ Clang Command Adapter plug-in is not configured to support C++11 by default,

you also need to configure c++11 support.

1.Click Code->Preferences->Settings, search for Clang: Cflags, Clang: Cxxflags and add C++11 configuration;

2.Clang: Cflags adds Item: "-std=c11", Clang: Cxxflags add Item: "-std=c++11".

Thirdly,

also, you need the C++11 running environment configuration.

  1. Click the gear of Code Runner⚙ (or right-click it directly) to open the extended settings;

  2. Find Code-runner: Executor Map and click Edit in settings.json;

  3. Find the line "cpp" in "code-runner.executorMap";

  4. Add -std=c++11 to the end of $fileNameWithoutExt and save it for consumption.

Or,

1.edit your Environment configuration ./vscode/setting.json/;

2.find "code-runner.executorMap":;

3.find "cpp": in "code-runner.executorMap":;

4.add -std=c++11 between $fileNameWithoutExt and && $dir$fileNameWithoutExt

just like:

enter image description here

like image 23
Vittore Marcas Avatar answered Sep 24 '22 06:09

Vittore Marcas