Skip to main content
GameDev.net gamedev.net
🔒 Locked

stl list,vector....

Started by Pactuul Jul 4, 2002 at 1:26 AM 11 replies 2.1k views
Original Post
Pactuul
Pactuul
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
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Gee
Gee
Nevermind my post... I see now your brackets where removed that's why list was not followed by < >

[edited by - Gee on July 4, 2002 3:17:05 AM]
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
DerekSaw
DerekSaw
From the few lines you post, it seems nothing wrong. Maybe problem is in other part of your code?
"after many years of singularity, i'm still searching n the event horizon" o
Pactuul
Pactuul
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?
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
DerekSaw
DerekSaw
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. Here''s what I can do with list:

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.
"after many years of singularity, i'm still searching n the event horizon" o
Pactuul
Pactuul
sorry I wrote that code by hand (but I obviously didn''t declare it twice sorry

I notice when you do this :
it->x = newX;

I was doing this:

(*it).x = newX;

(I realize that''s suppose to be equvilant to ->)
That seems like it might be accessing it differently.
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
DerekSaw
DerekSaw
quote:
Original post by Pactuul
...
(I realize that''s suppose to be equvilant to ->)
...


Aboslutely correct! It is the same. So... I think your problem lies somewhere else?
"after many years of singularity, i'm still searching n the event horizon" o
Dormeur
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...


  
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
SabreMan
SabreMan
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.
DerekSaw
DerekSaw
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?
"after many years of singularity, i'm still searching n the event horizon" o
Dormeur
Dormeur
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
SabreMan
SabreMan
I suggest Pactuul posts a small compilable example of the code that demonstrates the error, rather than guessing where the error is and only showing us a couple of lines.
Pactuul
Pactuul
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:


  
{
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!

-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.