我正在从编程:原理与实践使用C ++ / Edition 2学习c ++,而且我遇到了矢量问题。我使用了Stroustrup的书here提供的包含头文件。当我编译以下矢量程序时,我收到一个错误。
#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() { }
^
我正在编译:clang ++ -std = c ++ 11 -stdlib = libc ++ vector.cpp
当我查看我的Clang版本时,我得到:
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
我无法理解错误和警告,也不确定从何处开始。感谢您提供的任何见解。
答案 0 :(得分:1)
std_lib_facilities.h
将类Vector<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
构造函数。
此时,您的选择有限。
std_lib_facilities.h
#undef vector
std_lib_facilities.h
您可以替换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);
}
};
答案 1 :(得分:0)
链接的头文件包含以下行:
#define vector Vector
这意味着当你编写vector<int>
时,编译器会看到Vector<int>
,这是他自己的向量实现。我没有看到构造函数从硬编码数组构建实例,就像你想要做的那样。
如果包含实际的标准库,它应该可以工作,因为您在C ++ 11中进行编译(参见here)。虽然该向量不会在链接头文件中添加范围检查。