user2073370
user2073370

Reputation: 3

error creating a vector of structs from stl_vector.h (probably trivial)

In my code I have:

struct datapoint
{
  double energy;
  double probability;
};

Which is later put into a vector like so:

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;
...

However, at compile time, for the line

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

I receive the error

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

I'm a bit confused as I have done this before.

Upvotes: 0

Views: 482

Answers (2)

blackmath
blackmath

Reputation: 252

struct datapoint
{
    double energy;
    double probability;
    datapoint():energy(0.0), probability (0.0)
    {}
};

then std::vector spectrum(71);

Have fun,

Upvotes: 1

us2012
us2012

Reputation: 16253

You are trying to invoke the fill-constructor of vector:

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

But 0 is not a valid value of type datapoint.

Upvotes: 3

Related Questions