5

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?

user3650284
  • 137
  • 1
  • 3
  • 10
  • Did you `#include `? – Brian Bi Aug 09 '14 at 06:29
  • 1
    What's `Vector`, I mean with capital V? – 101010 Aug 09 '14 at 06:30
  • Yes, I did #include . I really don't know what these error messages mean, I'm newbie. – user3650284 Aug 09 '14 at 06:32
  • Try `std::vector v = {1,2,3};` and `std::vector v = {"one", "two", "three"};` and report back. – 101010 Aug 09 '14 at 06:33
  • Let me guess, is this book Bjarne Stroustrup's *Programming: Principles and Practices Using C++*? – T.C. Aug 09 '14 at 06:36
  • I tried examples 40two, and first one with int works, but second one gives an error identifier string is undefined. And yes, that's the book. – user3650284 Aug 09 '14 at 06:38
  • Make sure you are using [this version](http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h) of his `"std_lib_facilities.h"` header. – T.C. Aug 09 '14 at 06:39
  • I downloaded std_lib_facilities.h again, and just like that, now everything works like a dream. Thank you. – user3650284 Aug 09 '14 at 06:42
  • @40two "Disgusting macro hacks" strike again...(Bjarne's header has a `#define vector Vector` in it, which has been generating SO questions for a while.) – T.C. Aug 09 '14 at 06:43
  • @T.C. Should I edit the header file, or just leave it? – user3650284 Aug 09 '14 at 06:44
  • @user3650284 You can just use the new version of the header if you are learning C++ using the book. He used some hackish constructs in that header to simplify things for first-time learners. – T.C. Aug 09 '14 at 06:45
  • @T.C Looked like that. I have the same book just to decorate my bookshelves. Read the first chap and quit. – 101010 Aug 09 '14 at 06:47
  • @40two Why is that? Do you have some other recommendations (books)? – user3650284 Aug 09 '14 at 06:50
  • @user3650284 It's a good book, but its primary target audience is first-time programmers rather than experienced ones. If you are looking for addition book recommendations, check out [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – T.C. Aug 09 '14 at 06:53
  • @user3650284 Bjarne uses a lot of custom stuff in this book. I didn't like it because I've already had addiction in C++ when I bought it. I was looking for something more of professional C++ directions. In my opinion for beginners C++ primer is better. – 101010 Aug 09 '14 at 07:04

3 Answers3

5

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.

T.C.
  • 133,968
  • 17
  • 288
  • 421
1

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

Vittore Marcas
  • 1,007
  • 8
  • 11
0

Try to #include <string> to define string. And using namespace std;

The last line lets you skip std:: infront of stuff.

robbannn
  • 5,001
  • 1
  • 32
  • 47