How to pass the values stored in vector container to a new variable?

I want to pass the values contained in a vector to a new variable. But it seems that there is something wrong in type conversion, because repeatdly it throws the error:

       error: cannot convert 'float*' to 'double' in 
              initialization
      error: cannot convert 'std::vector<float>' to 
             'double' in initialization

I’ve tried to change the data types of the vector and the variable, but the error keep on coming!!

#include <boost/multi_array.hpp>
#include <h5xx/h5xx.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

using array_2d_t = boost::multi_array<float, 2>;

h5xx::dataset open_dataset(std::string const& filename) {
    h5xx::file xaa(filename, h5xx::file::mode::in);
    h5xx::group g(xaa, "particles/lipids/box/positions");
    return h5xx::dataset(g, "value");
}

std::vector<float> cell_from_all_frames(h5xx::dataset& ds, size_t row, size_t col) {
    // determine dataset shape: frames, particle count, space dimension
    auto ds_shape = h5xx::dataspace(ds).extents<3>();
    std::vector<float> cells(ds_shape[0]); // number of frames

    std::vector<hsize_t> offsets{0, row, col};
    std::vector<hsize_t> counts{ds_shape[0], 1, 1};
    h5xx::slice slice(offsets, counts);

    h5xx::read_dataset(ds, cells, slice);
    return cells;
}

int main(int argc, char const* argv[])
{
    if (argc < 2) {
        std::cout << "Usage: " << argv[0] << " input.h5" << std::endl;
        return -1;
    }

    auto ds = open_dataset(argv[1]);
    std::vector<float> first_cells = cell_from_all_frames(ds, 0, 0);
    size_t nsamples = first_cells.size();
    std::cout << "no. of samples: " << nsamples ;
    double sampling_interval = 1;       // time between samples

    correlator::multi_tau_correlator<double> corr(    // TODO replace sample type
        nsamples * sampling_interval / 30        // max lag time: fraction of total trajectory length
      , sampling_interval                        // time resolution at lowest level
      , 10                                       // block size   // FIXME pass as (optional) command line argument
    );

    // define time correlation functions
    auto msd = make_correlation(correlator::mean_square_displacement(), corr);
    corr.add_correlation(msd);

    // main loop
    for (size_t i = 1; i < nsamples; ++i) {
        double position_array = first_cells();    
        //double position_array = static_cast<double>(std::rand()) / RAND_MAX;
        std::cout << "position arrays: " << position_array << std::endl;
        // append data to the correlator, which possibly computes some time correlations
        corr.sample(position_array);
    }
    corr.finalise();

    return 0;
}

The issue lies in main() function under the comment main loop. I want to pass the values stored in first_cells to the position_array, but it throws the errors mentioned above. I have tried to pass the some random numbers and guess what, it works fine!

Answer

double position_array = first_cells();

This tries to invoke first_cells (a vector) as a callable (but it doesn’t implement operator(): https://en.cppreference.com/w/cpp/container/vector).

Assuming that the loop variable is there for a reason, why not use it:

double position_array = first_cells[i];

Note also

  • this does a possibly unwanted conversion of float to double

  • if you want bounds-checking, use

      double position_array = first_cells.at(i);
    

The name position_array does suggest some more confusion. Did you actually want the three-value tupled cell instead of just the first value?


Getting first rows instead of first cells:

auto       ds         = open_dataset(argv[1]);
array_2d_t first_rows = row_from_all_frames(ds, 0);

Which you can use:

size_t     nsamples   = first_rows.size();

std::cout << "no. of samples: " << nsamples << "n";

// main loop
for (size_t i = 1; i < nsamples; ++i) {
    auto position_array = first_rows[i];
    // double position_array = static_cast<double>(std::rand()) / RAND_MAX;
    std::cout << "position arrays: "       //
              << position_array[0] << ", " //
              << position_array[1] << ", " //
              << position_array[2] << std::endl;
    // append data to the correlator, which possibly computes some time
    // correlations
    //corr.sample(position_array);
}

Note that position_array is a sub array view from multi_array, so the interface is consistent with multi_array types.

Prints, on my system:

no. of samples: 75
position arrays: 80.03, 35.42, 4.35
position arrays: 80.19, 35.62, 4.27
position arrays: 79.78, 35.68, 4.13
position arrays: 79.51, 35.93, 4.1
position arrays: 79.44, 35.46, 4.27
position arrays: 79.5, 35.43, 4.38
position arrays: 79.03, 35.72, 4.54
position arrays: 79.12, 35.89, 4.28
position arrays: 79.04, 36.35, 3.99
position arrays: 79.06, 36.16, 4.52
position arrays: 79.22, 35.96, 4.39
position arrays: 79.07, 35.84, 4.28
position arrays: 79.43, 35.09, 4.4
position arrays: 79.38, 35.13, 3.81
position arrays: 78.87, 35.73, 4.54
position arrays: 79.3, 35.82, 4.33
position arrays: 79.38, 35.45, 3.98
position arrays: 79.5, 35.48, 3.88
position arrays: 79.16, 34.93, 4.35
position arrays: 78.86, 35.2, 4.44
position arrays: 79.15, 35.53, 4.08
position arrays: 79.41, 35.67, 3.87
position arrays: 79.83, 35.61, 4.19
position arrays: 79.63, 35.26, 3.86
position arrays: 79.94, 35.42, 4.11
position arrays: 80.32, 35.06, 4.01
position arrays: 79.99, 35.44, 3.97
position arrays: 79.82, 35.31, 4.07
position arrays: 80, 34.97, 4.07
position arrays: 80.22, 35.07, 3.91
position arrays: 80.38, 35.56, 3.92
position arrays: 80.6, 35.14, 4.11
position arrays: 80.57, 34.93, 4.15
position arrays: 80.05, 35.33, 4.46
position arrays: 80.12, 35.21, 4.2
position arrays: 80.12, 35.39, 3.97
position arrays: 80.19, 35.69, 4.18
position arrays: 80.4, 35, 3.96
position arrays: 80.55, 35.39, 4.26
position arrays: 80.52, 34.85, 4.07
position arrays: 80.57, 34.66, 4.04
position arrays: 80.69, 34.64, 4.05
position arrays: 80.94, 34.53, 3.88
position arrays: 81.12, 33.99, 4.22
position arrays: 81.25, 34.02, 4.08
position arrays: 81.68, 33.82, 4.17
position arrays: 81.75, 33.89, 4.35
position arrays: 82.2, 34.24, 4.28
position arrays: 81.83, 34.51, 4.17
position arrays: 82.17, 34.09, 4.35
position arrays: 82.33, 34.32, 4.3
position arrays: 82.65, 34.35, 4.09
position arrays: 82.44, 34.6, 4
position arrays: 82.51, 34.04, 4.41
position arrays: 82.4, 34.45, 4.34
position arrays: 81.89, 34.48, 4.18
position arrays: 81.59, 34.62, 4.14
position arrays: 81.82, 34.22, 4.34
position arrays: 81.43, 33.95, 4.05
position arrays: 81.35, 33.88, 3.9
position arrays: 81.44, 33.85, 4.24
position arrays: 81.48, 33.39, 4.25
position arrays: 81.51, 33.69, 4.16
position arrays: 81.66, 33.49, 4.34
position arrays: 82.1, 33.45, 4.17
position arrays: 82.61, 33.8, 4.07
position arrays: 82.51, 33.96, 4.5
position arrays: 82.36, 34.13, 4.46
position arrays: 82.46, 34.28, 4.19
position arrays: 82.59, 34, 4.17
position arrays: 82.26, 33.92, 4.44
position arrays: 82.2, 34.06, 4.18
position arrays: 82.24, 34.12, 4.29
position arrays: 82.16, 33.39, 3.94