gpt4 book ai didi

c++ - 从 STL_vector.h 创建结构 vector 时出错(可能微不足道)

转载 作者:行者123 更新时间:2023-11-27 23:18:34 24 4
gpt4 key购买 nike

在我的代码中我有:

struct datapoint
{
double energy;
double probability;
};

稍后将其放入 vector 中,如下所示:

std::vector<datapoint> spectrum(71,0);

spectrum[0].energy = 0.937729;
spectrum[0].probability = 0.0022582628449311468;
spectrum[1].energy = 1.875458;
spectrum[1].probability = 0.0033531784328108922;
...

然而,在编译时,对于行

std::vector<datapoint> spectrum(71,0);

我收到错误

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h: In member function âvoid std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = datapoint, _Alloc = std::allocator<datapoint>]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:303: instantiated from âstd::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = int, _Tp = datapoint, _Alloc = std::allocator<datapoint>]â
/src/PrimaryGeneratorAction.cc:74: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:991: error: no matching function for call to âstd::vector<datapoint, std::allocator<datapoint> >::_M_fill_initialize(size_t, int&)â

我有点困惑,因为我以前做过这个。

最佳答案

您正在尝试调用 vector 的填充构造函数:

explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());

但是 0 不是 datapoint 类型的有效值。

关于c++ - 从 STL_vector.h 创建结构 vector 时出错(可能微不足道),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14882891/

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