2016-11-05 84 views
1

我正在學習編程:使用C++ /版本2的原則和實踐中的C++,並且我遇到了向量問題。我正在使用Stroustrup的書here提供的附帶頭文件。當我編譯以下矢量程序時,出現錯誤。在Mac上沒有匹配構造函數初始化

#include "std_lib_facilities.h" 

int main() 
{ 
    vector<int> v = {5, 7, 9, 4, 6, 8}; 
    for (int i=0; i<v.size(); ++i) 
     cout << v[i] << endl; 
} 

錯誤

vector.cpp:5:21 error: no matching constructor for initialization of 'Vector<int>' 
    vector<int> v = {5, 7, 9, 4, 6, 8}; 
       ^ ~~~~~~~~~~~~~~~~~~ 
./std_lib_facilities.h:82:5: note: candidate constructor template not viable: 
     requires 2 arguments, but 6 were provided 
    Vector(I first, I last) :std::vector<T>(first, last) {} 
    ^
./std_lib_facilities.h:79:14: note: candidate constructor not viable: requires 2 
     arguments but 6 were provided 
    Vector(size_type n, const T& v) :std::vector<T>(n,v) {} 
    ^
./std_lib_facilities.h:79:14: note: candidate constructor not viable: requires 
     single argument 'n', but 6 arguments were provided 
    explicit Vector(size_type n) :std::vector<T>(n) {} 
./std_lib_facilities.h:75:27: note: candidate constructor (the implicit move 
     constructor) not viable: requires 1 argument, but 6 were provided 
    template< class T> struct Vector : public std::vector<T> { 
          ^
./std_lib_facilities.h:75:27: note: candidate constructor (the implicit copy 
    constructor) not viable: requires 1 argument but 6 were provided 
./std_lib_facilities.h:78:5: note: candidate constructor not viable: requires 0 
     arguments, but 6 were provided 
    Vector() { } 
    ^

我與編譯:鏗鏘++ -std = C++ 11 -stdlib =的libC++ vector.cpp

當我檢查我的版本鐺的,我得到:

Apple LLVM Version 8.0.0 (clang-800.0.42.1) 
Target: x86_64-apple-darwin16.1.0 
Thread model: posse 
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin 

我很難理解錯誤和警告,不知道從哪裏去。感謝您提供的任何見解。

+1

C++區分大小寫。 –

+0

@KerrekSB Stroustrup的'std_lib_facilities.h'具有'#define vector Vector'。 http://www.stroustrup.com/Programming/std_lib_facilities.h不能說,我是它的粉絲,雖然... – krzaq

+0

從你的編譯器使用STL –

回答

1

std_lib_facilities.hVector<T>類定義爲:

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 

正如你所看到的,沒有initializer_list構造。

在這一點上,您的選擇是有限的。

  1. 你可以避開使用std_lib_facilities.h
  2. 可以#undef vector包括std_lib_facilities.h
  3. 後,您可以更換Vector類模板的構造函數繼承構造函數(using vector<T>::vector;):

    template< class T> struct Vector : public std::vector<T> { 
        typedef typename std::vector<T>::size_type size_type; 
    
        using std::vector<T>::vector; 
    
        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); 
        } 
    }; 
    
+0

感謝您的快速回答,krzaq!我不明白頭文件,所以我不知道使用#undef矢量或什麼類模板構造函數。我繼續前進,並按照你所說的替換了Vector類模板,並且它可以工作。我只是不確定我替換了什麼,以及替換了它。 – mjdn

+0

@mjdn你可能會在下面的章節中學習:) – krzaq

0

鏈接的標題文件有以下行:

#define vector Vector

這意味着,當你寫vector<int>編譯器看到Vector<int>,這是他自己的實施載體。我沒有看到像你正在嘗試的那樣從一個硬編碼數組構建一個實例的構造函數。

如果您包含實際的標準庫,它應該工作,因爲您正在使用C++ 11編譯(請參閱here)。雖然該向量不會在鏈接的頭文件中添加範圍檢查。

相關問題