所以我想制作一个粒子系统,我需要创建一些粒子对象并将它们存储在一个向量中以便以后使用它们。 ,做的是功能是:创建一个对象并将其存储在向量中可以产生值
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));
}
}
颗粒类的构造函数如下:
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
}
但是,如果我试图遍历准确后,我完成存储它的载体,它给了我值为:1.81063e + 13。 我确实试图研究它很多,但找不到任何解决方案。 编辑:
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;
}
}
它没有发生在你身上它可能是后来的循环是这个问题? – StoryTeller
我必须质疑'std :: vector'的用法 - 它应该是非指针'std :: vector '或使用智能指针,例如:'std :: vector >'' –
UnholySheep
为什么你需要在该向量中存储指针,你不能简单地在那里存储普通的'粒子'实例吗? – user0042