🔒 Locked
stl list,vector....
Advertisement
Original Post
I''m having a problem accessing and changing info in a list (or a vector for that matter)
I have a list declared like this:
list Particles;
list::iterator particleIT;
put when I want to access information and change it like so:
particleIT = Particles.begin();
(*particleIT).SOMEVALUE = WHATEVER;
It doesn''t change that value in the list. It''s not causing any compiler errors, and the list is not empty. So is there some concept i''m missing with changing and accessing elements of a list?
Thanks!
Pac
whoops sorry about the brackets...( they are there.)
I tried to debug those lines:
particleIT = Particles.begin();
(*particleIT).SOMEVALUE = WHATEVER;
by doing this :
particleIT = Particles.begin();
int temp = (*particleIT).SOMEVALUE;
(*particleIT).SOMEVALUE = WHATEVER;
int temp = (*particleIT).SOMEVALUE;
and then watching what happens to temp..and nothing changedd even though it clearly should have. I also tried making it a list of pointers, at which (by editing a few lines of code) it would compile, but then I get memory access errors?
I tried to debug those lines:
particleIT = Particles.begin();
(*particleIT).SOMEVALUE = WHATEVER;
by doing this :
particleIT = Particles.begin();
int temp = (*particleIT).SOMEVALUE;
(*particleIT).SOMEVALUE = WHATEVER;
int temp = (*particleIT).SOMEVALUE;
and then watching what happens to temp..and nothing changedd even though it clearly should have. I also tried making it a list of pointers, at which (by editing a few lines of code) it would compile, but then I get memory access errors?
quote:
Original post by Pactuul
particleIT = Particles.begin();
int temp = (*particleIT).SOMEVALUE;
(*particleIT).SOMEVALUE = WHATEVER;
int temp = (*particleIT).SOMEVALUE;
The compiler should complain about redefinition of ''temp'' because you declare them twice. It is wierd to have no error (or warning) on that. Anyway not your main problem.
struct Particle
{
float x, y, z, time;
};
...
typedef std::list<Particle> ParticleList;
Particle p;
ParticleList particleList;
ParticleList::iterator it;
...
p.x = randomX;
particleList.push_back(p);
...
it = particleList.begin();
it->x = newX;
cout << it->x;
quote:
... I also tried making it a list of pointers, at which (by editing a few lines of code) it would compile, but then I get memory access errors?
For pointers, things could be a different a little. You have to new them when you ''push_back'' and delete them when you not needing them anymore.
Maybe it''s because you haven''t overloaded the ''='' operator in your Particles struct? the list will use it to copy its elements back and forth, so...
Hope this helps
Dormeur
struct Particle {
float x,y,z,time;
// Default constructor
Particle() {}
// Copy constructor
Particle(const Particle& src) {
copy(src);
}
// The overloaded operator
Particle& operator = (const Particle& src) {
copy(src);
}
// While we''re at it: let''s overload the ''=='' operator too
bool operator == (const Particle& src) const {
return (x == src.x && y == src.y && z == src.z && time == src.time);
}
// Helper method
void copy(const Particle& src) {
x = src.x; y = src.y; z = src.z; time = src.time;
}
}
Hope this helps
Dormeur
quote:
Original post by Dormeur
Maybe it''s because you haven''t overloaded the ''='' operator in your Particles struct?
In the class you''ve posted, the default assignment operator will do the right thing.
quote:
While we''re at it: let''s overload the ''=='' operator too
Let''s not.
quote:
Original post by Dormeur
Maybe it''s because you haven''t overloaded the ''='' operator in your Particles struct? the list will use it to copy its elements back and forth, so...
Dormeur
There''s no need to overload. He''s doing member-by-member assignment.... unless the members are some pointers or references... or is it Pactuul?
Yep, you guys are right about the overloading thing.
But I just wrote a little test app, and it works fine, Pactuul. It must be some other problem.
Also note that when you add an instance of Particle to the list, it is copied, so modifying the original object will not modify the entry in the list, and vice versa!
Dormeur
But I just wrote a little test app, and it works fine, Pactuul. It must be some other problem.
Also note that when you add an instance of Particle to the list, it is copied, so modifying the original object will not modify the entry in the list, and vice versa!
Dormeur
ok i did figure out what the reason was for my error, and it wasn''t related to stl 
what happened it seems is I was updating a particle by moving it in it''s specified direction every .1 seconds. (to slow it down) like so:
well, this code caused the particles age to increase by the amount timepassed since the last frame, but this was only done only every .1 seconds (the timepassed was usally like .01, so its age would take a long time to hit 5.
But what I thought was wierd is when it did it should have waited another 5 to reverse directions again, well stupid me forgot reset the age to 0
I thought this was being masked by stl, because when I went to debug and step through what was happening, I could for some reason get the values from a particle, thats why I was wandering about the "int temp = whatever" thing, but now afer a goodnights sleep, it works correctly now. Plus I''ve had problems before with stl and its using of pointers, so it became a big confusion to me. But i did sort out previous issues i''ve had by your comments!
what happened it seems is I was updating a particle by moving it in it''s specified direction every .1 seconds. (to slow it down) like so:
{
static float total = 0;
D3DXVECTOR3 tempvector;
total += timepassed;
if(total > 0.1f)
{
for( particleIT= Particles.begin(); particleIT != Particles.end(); particleIT++ )
{
(particleIT)->age += timepassed;
if((particleIT)->age >= 5)
{
tempvector = (particleIT)->vDir = (particleIT)->vDir * -1;
}
(particleIT)->vPos += (particleIT)->vDir*timepassed;
}
total = 0;
}
};
well, this code caused the particles age to increase by the amount timepassed since the last frame, but this was only done only every .1 seconds (the timepassed was usally like .01, so its age would take a long time to hit 5.
But what I thought was wierd is when it did it should have waited another 5 to reverse directions again, well stupid me forgot reset the age to 0
I thought this was being masked by stl, because when I went to debug and step through what was happening, I could for some reason get the values from a particle, thats why I was wandering about the "int temp = whatever" thing, but now afer a goodnights sleep, it works correctly now. Plus I''ve had problems before with stl and its using of pointers, so it became a big confusion to me. But i did sort out previous issues i''ve had by your comments!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement