gpt4 book ai didi

c++ - Mac 上没有匹配的初始化构造函数

转载 作者:行者123 更新时间:2023-11-30 05:20:55 25 4
gpt4 key购买 nike

我正在通过编程学习 C++:使用 C++ 的原理和实践/第 2 版,我遇到了 vector 问题。我正在使用 Stroustrup 的书 here 中提供的头文件.当我编译下面的 vector 程序时,出现错误。

#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

我无法理解错误和警告,也不确定从这里该何去何从。感谢您提供的任何见解。

最佳答案

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构造函数。

此时,您的选择是有限的。

  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);
    }
    };

关于c++ - Mac 上没有匹配的初始化构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com