gpt4 book ai didi

c++ - 使用带有初始化列表的 Stroustrup 的 PPP 书中的 vector 时出现编译错误

转载 作者:行者123 更新时间:2023-11-30 01:40:56 25 4
gpt4 key购买 nike

我的代码:

#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)

我该如何解决这个问题?

最佳答案

看起来您使用了错误版本的 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

关于c++ - 使用带有初始化列表的 Stroustrup 的 PPP 书中的 vector 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392016/

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