-4

So I am trying to make a particle system and I need to create some Particle objects and store them in a vector to use them later. The function that does that is:

void spawnLoop( std::vector<Particle*> &particleVector ){
    for (int i=0; i < 5; i++) {
        particleVector.emplace_back( new Particle(400.0, 400.0, 1.0, 1.0) );
    }
}

The particle class constructor looks like this:

Particle::Particle(float xPos= 400,float yPos= 400,float xVel= 0,float yVel= 0) {
float xPosition = xPos;
float yPosition = yPos;
float xVelocity = xVel;
float yVelocity = yVel;
bool dead = false;
std::cout<< "We have " << xPosition  << " "<< yPosition << " "<< xVelocity << " "<< yVelocity << std::endl;
//This prints the values and they look correct
}

But if I try to loop over the vector exactly after I finish to store it , it gives me values as:1.81063e+13. I did try to research it for quite a bit but couldn't find any solution to it. EDIT:

void loopOver( std::vector<Particle*> const vec ){
for (auto i = vec.begin(); i != vec.end(); i++){
    std::cout << "avem " << (*i)->getXPos() << " " << (*i)->getYPos() << std::endl;
}
}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • 1
    Didn't it occur to you it may be the later looping that's the problem? – StoryTeller - Unslander Monica Sep 24 '17 at 07:51
  • 1
    I have to question the usage of `std::vector` - it should be either non-pointer `std::vector` or using smart pointers, e.g.: `std::vector>` – UnholySheep Sep 24 '17 at 07:52
  • Why do you need to store pointers in that vector, can't you simply store plain `Particle` instances there? – user0042 Sep 24 '17 at 07:52
  • @StoryTeller , added how the looping is looking like. I don't see if it's wrong or not. I was editing this reponse and took quite a bit to form a good response. songyuanyao, I want to store them for later use. –  Sep 24 '17 at 07:53
  • 5
    You're initializing 4 local variables in the constructor. Is that your intent? – songyuanyao Sep 24 '17 at 07:54
  • 1
    So what are `getXPos` and `getYPos` returning? as @songyuanyao points out, your constructor is not initializing any member variables – UnholySheep Sep 24 '17 at 08:00
  • @UnholySheep, They are returning the private variables xPosition and yPosition. As with the constructor, I don't really understand what you are saying. Also, sorry if this is wasting your time since I see that it got quite a bit of negative points. –  Sep 24 '17 at 08:02
  • The only `xPosition` and `yPosition` you have shown in your code are local variables inside you constructor - they are not stored in the object itself. So your two getters are quite likely returning the (uninitialized) member variables (which invokes *undefined behavior*) - remove the `float` from `float xPosition` in your constructor (and the same for the other 4 variables) and it should work – UnholySheep Sep 24 '17 at 08:04
  • 2
    As an occasional SO user, I'm curious why this question has been downvoted. Would one of the downvoters care to explain? It seems to me to be a pretty clear question that contains enough of the code to analyze it and even figure out pretty quickly where the problem is. If it was downvoted before he added the loop code, then I think the downvote should be removed. – Eli Sep 24 '17 at 08:10
  • @Eli One candidate could be lack or research effort. It wouldn't take too much to figure out the problem has nothing to do with storing things in a vector, for example. – juanchopanza Sep 24 '17 at 09:04
  • @juanchopanza, I did research the subject but the problem was that I didn't know where exactly to search. It wasn't intentional to make the question bad. I gave my best and people still seem to give me down votes. –  Sep 24 '17 at 10:51
  • @Turry You could have easily checked if the vector had anything to do with the problem. – juanchopanza Sep 24 '17 at 16:22
  • @juanchopanza, I did try to check it but I thought I was either looping over it badly or I said the problem is when storing them in the vector because everything looked ok before adding them to the vector. Didn't know it was from the Object Cpnstructor.. –  Sep 24 '17 at 17:30
  • @juanchopanza It's a judgement call but I disagree. Almost any question on SO could have been researched "better" by the asker, but of course it depends on the asker's level of experience, etc. In this case, the question appears perfectly reasonable to me. Accidentally declaring an instance variable as a local variable can be a tricky hidden bug that many developers have done in their careers. – Eli Sep 25 '17 at 08:06
  • @Eli It is basic problem solving, not restricted to programming. You think you have a problem with X in a system with X, Y and Z. First step, remove X and see if the problem goes away. But yes, most questions posted in the last few years are poorly researched. Answering them only encourages more of them. – juanchopanza Sep 25 '17 at 09:29

1 Answers1

1

The problem is in the constructor of Particle. You are assigning the parameters to local variables, not to instance variables. So, later, when you try to print out the values of those variables, they have not been saved:

This is the problem:

float xPosition = xPos;
float yPosition = yPos;
float xVelocity = xVel;
float yVelocity = yVel;

It should be something like this:

_xPosition = xPos;
_yPosition = yPos;
_xVelocity = xVel;
_yVelocity = yVel;

Where all those variables are defined as instance variables of the class, and getXPos(), etc. return them.

Even better would be to not assign the values in the constructor, but to initialize them an initialization list. Here's a decent question/answer about initialization lists: C++ initialization lists for multiple variables

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Eli
  • 693
  • 5
  • 12
  • 1
    It didn't really work with the _ but if I just remove the float it will work. –  Sep 24 '17 at 08:09
  • @Turry The underscore is just a convention for naming instance variables. You can call them whatever you like. – Eli Sep 24 '17 at 08:11