C++ 从stl_vector.h创建结构向量时出错(可能很小)
在我的代码中,我有:C++ 从stl_vector.h创建结构向量时出错(可能很小),c++,vector,struct,C++,Vector,Struct,在我的代码中,我有: struct datapoint { double energy; double probability; }; 然后将其放入一个向量中,如下所示: std::vector<datapoint> spectrum(71,0); spectrum[0].energy = 0.937729; spectrum[0].probability = 0.0022582628449311468; spectrum[1].energy = 1.875458; sp
struct datapoint
{
double energy;
double probability;
};
然后将其放入一个向量中,如下所示:
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::向量谱(71,0);
光谱[0],能量=0.937729;
谱[0],概率=0.0022582628449311468;
光谱[1],能量=1.875458;
谱[1],概率=0.0033531784328108922;
...
但是,在编译时,对于
std::vector<datapoint> spectrum(71,0);
std::向量谱(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&)â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:在成员函数中–void std::vector::_M_initialize_dispatch(_Integer,_Integer,std:_true_type)[with _Integer=int,_tpdatapoint,_Alloc=std::allocator]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../../include/c++/4.4.6/bits/stl_vector.h:303:从–std::vector::vector(\u InputIterator,\u InputIterator,const Alloc&)实例化[with\u InputIterator=int,\u Tp=datapoint,\u Alloc=std::allocator]
/src/PrimaryGeneratorAction.cc:74:从此处实例化
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../../include/c++/4.4.6/bits/stl_vector.h:991:错误:调用–std::vector时没有匹配的函数::_M_fill_initialize(size_t,int&)
我有点困惑,因为我以前做过这件事。您正试图调用
向量的填充构造函数。
:
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
但是
0
不是类型为datapoint
的有效值 您正试图调用向量的填充构造函数:
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
struct datapoint
{
double energy;
double probability;
datapoint():energy(0.0), probability (0.0)
{}
};
但是
0
不是类型为datapoint
的有效值
struct datapoint
{
double energy;
double probability;
datapoint():energy(0.0), probability (0.0)
{}
};
然后
std::向量谱(71)
玩得开心
然后
std::向量谱(71)
玩得开心点,
0
不是类型为datapoint
的有效值。您在这里想做什么std::vector spectrum(71,0)代码>?0
不是类型为datapoint
的有效值。您在这里试图做什么std::vector spectrum(71,0)代码>?对C++11很好,在旧版本中定义构造函数意味着类型不再是POD时要小心-代码的其他部分可能依赖于此。有关详细信息,请参阅。其他部分如何对此作出答复?这难道不是他们考虑旧数据风格的错误吗?这是什么样的论点?假设你使用的是一个大系统,外部库,等等,你不能完全像“我要改变它,我不在乎它是否坏了,因为它是所有其他人的错,没有以其他方式做它在第一位”。与C++11很好,在旧版本中,如果定义构造函数意味着类型不再是POD,请小心-代码的其他部分可能依赖于此。有关详细信息,请参阅。其他部分如何对此作出答复?这难道不是他们考虑旧数据风格的错误吗?这是什么样的论点?假设你正在使用一个大系统、外部库等——你不能完全像“我要改变这个,我不在乎它是否坏了,因为它是其他人的错,因为没有从一开始就用另一种方式去做。”。