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

£可爱£侵袭症+ 提交于 2019-12-12 17:41:01

问题


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.



来源:https://stackoverflow.com/questions/25215882/simple-code-need-help-no-instance-of-constructor-matches-argument-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!