Jump to content
  • Advertisement
Sign in to follow this  
Dolf

Vector question

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i am using a grid for collision detection. My question is how can i properly make an array of pointers ,using vectors that is,the way i add to the array like i do here.
//in my game:
gridarray[pos.x/10][pos.y/10]->AddToMe(&builds,0);


//in my classes
	std::vector<building> *buildings;
	std::vector<unit> *units;
	void AddToMe(building* build,unit* un)
	{
		if (build==0)
		{
			units->push_back(*un);
		}
		if (un==0)
		{
			buildings->push_back(*build);
		}
	}

as you can see, i subdevided my RTS game into buildings and units from which i derrive all other things in the universe ^^ But it's giving me errors

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Dolf
My question is how can i properly make an array of pointers ,using vectors that is


std::vector<variable*> pointer_vector;

variable var;
pointer_vector.push_back(&var);

Note that vectors do not clean up after you; if these objects are dynamically allocated (i.e., new'd), you are responsible for deleting them when you are done.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dolf
But it's giving me errors


A man once picked up his phone and dialed 911. When an officer answered, the man said "Hello, I'm calling you from the corner of 3rd Avenue and main street. I have a problem." He then hung up and waited for the police.

Either way, you're creating pointers to vectors. Why not vector instances? Also, since you're tying to store pointers in your vectors, they should be vectors of pointers, not vectors of instances.

Share this post


Link to post
Share on other sites

std::vector<building*> buildings;
std::vector<unit*> units;
void AddToMe(building* build,unit* un)
{
if (build==0)
{
units.push_back(un);
}
if (un==0)
{
buildings.push_back(build);
}
}




error: Unhandled exception at 0x00413fd6 in tmpl82.01.exe: 0xC0000005: Access violation reading location 0x00000008.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dolf
*** Source Snippet Removed ***

error: Unhandled exception at 0x00413fd6 in tmpl82.01.exe: 0xC0000005: Access violation reading location 0x00000008.


Edit: Nevermind, I can't read code today. See ToohrVyk's response for something that makes sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dolf
error: Unhandled exception at 0x00413fd6 in tmpl82.01.exe: 0xC0000005: Access violation reading location 0x00000008.


0. You can overload functions. There's no point in having a single function with an unused argument, when you can have two functions with one relevant argument each (that could be passed by reference to further simplify things).

1. The debugger should tell you which line (in your code) is responsible for that access violation. Which line is it?

2. Are all the objects used on that particular line (or anywhere else in your program, for that matter) fully and correctly initialized before they are used? You seem to have an unusually high number of pointers in there, so I would be wary if I were you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Look at what you're code is doing: if your pointers are NULL (or, to be pedantic, 0, which may not be NULL), add them to the vector...

Why would you want a vector full of NULL pointers?


Read his code again [wink] he adds the pointer which is (probably) not null to the vector every time.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Read his code again [wink] he adds the pointer which is (probably) not null to the vector every time.


Yeah, I'm having an off day...

Maybe I just need more coffee.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Look at what you're code is doing: if your pointers are NULL (or, to be pedantic, 0, which may not be NULL), add them to the vector...


He's actually pushing back the opposite pointer. i.e. if un is null then push building. or whatever. it's a really bad function. what if both are NULL then you'll push 2 NULL pointers...

Anyway, it's crashing likely because the this pointer is NULL. i.e. the class to which you are calling AddToMe hasn't actually been instanced. post the code where you are trying to add items to the queue.

-me

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!