我正在學習編程:使用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
我很難理解錯誤和警告,不知道從哪裏去。感謝您提供的任何見解。
C++區分大小寫。 –
@KerrekSB Stroustrup的'std_lib_facilities.h'具有'#define vector Vector'。 http://www.stroustrup.com/Programming/std_lib_facilities.h不能說,我是它的粉絲,雖然... – krzaq
從你的編譯器使用STL –