2017-02-22 41 views
1

我的代码:编译错误使用矢量时,从Stroustrup的PPP书初始化列表

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 
#include "c:\Users\1\documents\visual studio 2015\std_lib_facilities.h" 

int main() 
{ 
    vector<int> v = { 5, 7, 9, 4, 6, 8 }; 
    return 0; 
} 

失败,此错误编译:

Severity Code Description Project File Line Suppression State Detail Description 
Error (active)  no instance of constructor "Vector<T>::Vector [with T=int]" matches the argument list ConsoleApplication1 c:\Users\1\Documents\Visual Studio 2015\Projects\ConsoleApplication1\Source.cpp 6     argument types are: (int, int, int, int, int, int) 

我该如何解决这个问题?

+1

是C++ 11的编译器启用? –

+0

什么是'std_lib_facilities.h'? – Angew

+0

@πάνταῥεῖ无法在VS中禁用它;如果OP真的在使用VS2015,他们有'{}'支持。 – Angew

回答

2

看起来像你使用错误版本的std_lib_facilities.h。 Vector从此std_lib_facilities.h没有initializer_list构造函数。所以你不能用这个版本的头文件来做到这一点。

// trivially range-checked vector (no iterator checking): 
template< class T> struct Vector : public std::vector<T> { 
    typedef typename std::vector<T>::size_type size_type; 

    Vector() { } 
    explicit Vector(size_type n) :std::vector<T>(n) {} 
    Vector(size_type n, const T& v) :std::vector<T>(n,v) {} 
    template <class I> 
    Vector(I first, I last) :std::vector<T>(first,last) {} 

    T& operator[](unsigned int i) // rather than return at(i); 
    { 
     if (i<0||this->size()<=i) throw Range_error(i); 
     return std::vector<T>::operator[](i); 
    } 
    const T& operator[](unsigned int i) const 
    { 
     if (i<0||this->size()<=i) throw Range_error(i); 
     return std::vector<T>::operator[](i); 
    } 
}; 

// disgusting macro hack to get a range checked vector: 
#define vector Vector 
2

您正在使用Stroustrup的报头的版本错误。您使用的是PPP的第一版,使用here中的新版本,代码正常工作。

那么你也可以摆脱传统的固定

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS