Home » Community » Forums » General Programming » stl list,vector....
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 stl list,vector....
Post New Topic  Post Reply 
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

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

From the few lines you post, it seems nothing wrong. Maybe problem is in other part of your code?


 User Rating: 1094   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1094   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?

 User Rating: 1094   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?


 User Rating: 1094   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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!



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: