0

i'm new to C++ trying to learn parallel programming (coming from Basic), got stuck fairly early on.

class Particle{
private:
    double p_x, p_y, v_x, v_y, mass
public:
    Particle(double px, double py, double vx, double vy, double m) : p_x(px), p_y(px), v_x(vx), v_y(vx), mass(m) {};
    vector<int> pos () {p_x, p_y}; //doesn't work, expects ';'
    vector<int> vel () {v_x, v_y}; //doesn't work, expects ';'
};

I'm trying to create a class with properties pos and vel, both vectors. HNothing worked with what i'm trying to do - initializing vectors i guess.

Can anyone tell me how to make that work? Or if not that something like this:

class Particle{
    private:
        double p_x, p_y, v_x, v_y, mass
    public:
        Particle(double px, double py, double vx, double vy, double m) : p_x(px), p_y(px), v_x(vx), v_y(vx), mass(m) {};
        void SetPos(int x, int y) //pseudo code based on Basic
        void GetPos() as Vector   //pseudo code based on Basic
    };

Thanks in advance for your time, this has been a brick wall for me for a while. I've looked trough many other threads like this one around here but I don't know enough to adapt any of it to my needs i guess. To complicate things I'm using VS2012 Cuda 6.0 project which sometimes even acts differently than the standard C++ project. Reverted to 6.0 because chrono refused to work in 6.5. Would use the standard project but I don't know how (if possible) to integrate Cuda into it.

  • If you want the vectors to be data members, you need `vector pos{some_int, some_other_int};`. If not, you should clarify your question. – juanchopanza Oct 13 '14 at 17:12
  • This question appears to be off-topic because the sample contains too many typographical errors, that the code could be fixed to make sense. – πάντα ῥεῖ Oct 13 '14 at 17:18
  • No you can't initialize member variables "inline" like that, you hve to use an initializer list in the constructor. It has nothing to do with Cuda, it's how the C++ language is specified. – Some programmer dude Oct 13 '14 at 17:22
  • Have a look into the below: http://stackoverflow.com/questions/2236197/how-to-initialize-an-stl-vector-with-hardcoded-elements-in-the-easiest-way – iampranabroy Oct 13 '14 at 17:30

1 Answers1

1

The () indicates that they're functions, not variables; and the rest of the syntax isn't valid for a function definition. Either make them variables:

vector<int> pos {p_x, p_y};

or valid functions:

vector<int> pos () {return {p_x, p_y};}

You probably want them to be functions, so that they don't duplicate the values of the other members, and give the expected results if the other members are modified.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • One problem with the first example is that the `p_` are also data members, and not initialized at that point. – juanchopanza Oct 13 '14 at 17:14
  • @juanchopanza: Perhaps; reading the code again it looks like they should (probably) be functions. But they are initialised after the other members, so (unless I'm missing something) will take their values from them. – Mike Seymour Oct 13 '14 at 17:15
  • Actually, I was pretty sure the data member inline initialization happens before the initializer list initialization. – juanchopanza Oct 13 '14 at 17:57
  • @juanchopanza: No, "non-static data members are initialized in the order they were declared in the class definition (C++11 12.6.2/10)". An inline initialiser specifies how it's initialised (unless the constructor specifies otherwise), but not when. – Mike Seymour Oct 14 '14 at 12:40
  • But the `p_` and `v_` are doubles, so formally they are default initialized but reading from them is UB. I can't see how `pos` and `vel` could be safely initialized from them in this way. – juanchopanza Oct 14 '14 at 12:44
  • @juanchopanza: The constructor initialises the `double` members with its parameters, as specified by the initialiser list. They're declared before the `vector` members, so are initialised before them. – Mike Seymour Oct 14 '14 at 12:47
  • That makes sense. I'll have to re-read the relevant section of the standard. – juanchopanza Oct 14 '14 at 12:54