Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ can't put data into vector

I have a data-structure and a processor-class for the data, the data is stacked without pointers for faster SIMD processing:

struct trajectory_data {
    float position[3];
    float velocity[3];
    float acceleration[3];
    ...
};

class trajectory_processor{
private:
    vector<trajectory_data> tdata;
    vector<trajectory_data> default_data;
    ...
};

But I fail to actually add a data-set to the vector, neither of those work:

trajectory_processor::trajectory_processor(){

    // gives error: no match for ‘operator=’ in ...
    trajectory_data d0();
    default_data[0] = d0;

    // gives error: no matching function for call to
    // ‘std::vector<trajectory_data>::push_back(trajectory_data (&)())
    trajectory_data d1();
    default_data.push_back(d1);
};

According to push_back reference and C++ vector push_back I assumed this should be easy, but even after several google searches I just can't find any answer.

This project involves cross-coding in html/javascript and I seem to hit a wall like this one every time I switch back to c++, it starts wearing on my nerves.

like image 231
havarc Avatar asked Jun 20 '13 15:06

havarc


2 Answers

You seem to be a victim of the Most Vexing Parse. Basically, the line

trajectory_data d1();

is actually declaring a function d1 that takes no argument and returns a trajectory_data object.

Changing it to

trajectory_data d1;

should fix your problem, same for d0. The default constructor will be called anyways, no need for the ().

like image 50
anthonyvd Avatar answered Nov 15 '22 05:11

anthonyvd


Try this: default_data.push_back(trajectory_data());

like image 34
user1764961 Avatar answered Nov 15 '22 05:11

user1764961