0

在我的代码中,我有:

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::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&)â

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

4

2 回答 2

3

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

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

0不是 type 的有效值datapoint

于 2013-02-14T19:55:31.673 回答
1
struct datapoint
{
    double energy;
    double probability;
    datapoint():energy(0.0), probability (0.0)
    {}
};

然后 std::vector 谱(71);

玩得开心,

于 2013-02-14T20:13:36.333 回答