5

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.

Community
  • 1
  • 1
havarc
  • 934
  • 5
  • 11
  • 4
    I may be wrong here, but I think this is a subtle C++ parsing error with the line trajectory_data d0();. This is being interpreted as a function declaration with the function being called d0 and returning a trajectory_data. Try and remove the (). – Muckle_ewe Jun 20 '13 at 15:51

3 Answers3

8

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 ().

anthonyvd
  • 7,329
  • 4
  • 30
  • 51
  • 1
    Although, if you're relying on value-initialisation, you'll need to change it to `trajectory_data d1 = trajectory_data();` or `trajectory_data d1 {};` if you can use C++11. – Mike Seymour Jun 20 '13 at 15:55
  • Thanks, guess I shouldn't try that hard after only four hours of sleep. You just prevented a coder from giving up c++ and skipping to node.js :) – havarc Jun 20 '13 at 18:26
1

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

user1764961
  • 673
  • 7
  • 21
0

Change trajectory_data d0(); to trajectory_data d0;

The first declares a function d0 that takes no arguments and returns trajectory_data.
The second explicitly and clearly creates an instance of trajectory_data called d0.

This also applies to trajectory_data d1();

user123
  • 8,970
  • 2
  • 31
  • 52