|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| stl list,vector.... |
|
![]() Pactuul Member since: 2/20/2001 From: USA |
||||
|
|
||||
| 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 list 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 |
||||
|
||||
![]() Gee Member since: 3/22/2002 From: Stockholm, Sweden |
||||
|
|
||||
| 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] |
||||
|
||||
![]() DerekSaw Member since: 11/26/1999 From: 5-Claw, Dragonland |
||||
|
|
||||
| From the few lines you post, it seems nothing wrong. Maybe problem is in other part of your code? |
||||
|
||||
![]() Pactuul Member since: 2/20/2001 From: USA |
||||
|
|
||||
| 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? |
||||
|
||||
![]() DerekSaw Member since: 11/26/1999 From: 5-Claw, Dragonland |
||||
|
|
||||
quote: 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: 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. |
||||
|
||||
![]() Pactuul Member since: 2/20/2001 From: USA |
||||
|
|
||||
| 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. |
||||
|
||||
![]() DerekSaw Member since: 11/26/1999 From: 5-Claw, Dragonland |
||||
|
|
||||
quote: Aboslutely correct! It is the same. So... I think your problem lies somewhere else? |
||||
|
||||
![]() Dormeur Member since: 4/14/2000 From: Roeselare, Belgium |
||||
|
|
||||
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 Member since: 2/12/2002 |
||||
|
|
||||
quote: In the class you've posted, the default assignment operator will do the right thing. quote: Let's not. |
||||
|
||||
![]() DerekSaw Member since: 11/26/1999 From: 5-Claw, Dragonland |
||||
|
|
||||
quote: 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? |
||||
|
||||
![]() Dormeur Member since: 4/14/2000 From: Roeselare, Belgium |
||||
|
|
||||
| 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 Member since: 2/12/2002 |
||||
|
|
||||
| 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 Member since: 2/20/2001 From: USA |
||||
|
|
||||
| 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! |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|