• 13
• 18
• 19
• 27
• 9

Vector question

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

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:

//in my classes
std::vector<building> *buildings;
std::vector<unit> *units;
{
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 on other sites
Quote:
 Original post by DolfMy 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.

ah thanks man

Share on other sites
Quote:
 Original post by DolfBut 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 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 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 on other sites
Quote:
 Original post by Dolferror: 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 on other sites
Quote:
 Original post by Driv3MeFarLook 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 on other sites
Quote:
 Original post by ToohrVykRead 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 on other sites
Quote:
 Original post by Driv3MeFarLook 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