Simple Code Need Help - no instance of constructor

2019-07-22 04:12发布

问题:

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?

回答1:

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.



回答2:

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

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