Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

pointer problems

This topic is 3790 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

What am I doing wrong here? I'm trying to get my function to return a pointer to an object but I'm getting a syntax compile error :(
void* instance_nearest(int object_id, float x, float y, int not = -1)
{
	std::vector<object*>::iterator it;
	double dist = -1;
	int id;

	for (it = object_list.begin(); it != object_list.end(); it++)
	{
		if ((*it)->active == true && (*it)->object_id == object_id && (*it)-> id != not)
		{
			double tmpdist = point_distance(x, y, (*it)->x, (*it)->y);

			if (dist == -1 || tmpdist < dist)
			{
				dist = tmpdist;
				id = (*it) -> id;
			}
		}
	}
	return cgm::object_list[id];
}


obj_ball* near = instance_nearest(0, 0, 0);//error C2059: syntax error : '='


Share this post


Link to post
Share on other sites
Advertisement
You declared a return type of void, but you want a pointer to an object.
You want to return the address of the object you wish to point to, i.e.

Object* instance_nearest(...){ ... return &yourObject }

The reason you're getting an error is because obj_ball* near is expecting the address of an obj_ball, but your function is of return type void, which means that it in fact does not return anything. You're trying to use it (the function) as if it will return something, but it doesn't.

EDIT: BAD REPLY; Forgot about void pointers

[Edited by - Shakedown on February 5, 2008 11:00:21 AM]

Share this post


Link to post
Share on other sites
This gives the exact same error... (btw object_list is a vector containing <object*>

object* instance_nearest(int object_id, float x, float y, int not = -1)
{
std::vector<object*>::iterator it;
double dist = -1;
int id;

for (it = object_list.begin(); it != object_list.end(); it++)
{
if ((*it)->active == true && (*it)->object_id == object_id && (*it)-> id != not)
{
double tmpdist = point_distance(x, y, (*it)->x, (*it)->y);

if (dist == -1 || tmpdist < dist)
{
dist = tmpdist;
id = (*it) -> id;
}
}
}
return object_list[id];
}
...
object* near = instance_nearest(0, 0, 0);

Share this post


Link to post
Share on other sites
That is incorrect Shakedown

The function does return a value and that is a void*.
You should cast the returned value to obj_ball*.
Though I would have expected the error message to be about converting between
void* and obj_ball* ...

Also look around the line
obj_ball* near = instance_nearest(0, 0, 0);//error C2059: syntax error : '='
for a missing ; or other syntax errors.

Share this post


Link to post
Share on other sites
Quote:

The function does return a value and that is a void*.


Ah yes, I've long since forgotten about the void pointer. So you're going to need to explicitly cast your void* to the type you want.

@ Sync Views

By Object* instance_nearest(...) I don't mean literally use Object*, I meant use whatever type your game object is that you're dealing with. If you're using obj_ball, then enter obj_ball* instance_nearest(...).

Share this post


Link to post
Share on other sites
hmm...does vs2008 use "near" for something? I changed the var name on the off chance and it gave me a more useful error (about void* and obj_ball* being "unrelated") which I then fixed by adding a cast. near isn't highlighted or anything and Ive not used it anywhere else in my code (just did a search to make sure)...

Share this post


Link to post
Share on other sites
near and far are artifacts of a forgotten age, where a pointer could either be a near pointer, pointing at memory in your segment, or a far pointer pointing to memory further away.
These are irrelevant today, but exist as defines to nothing to allow old code to survive.
Thanks to the wonders of macros, your line becomes
obj_ball*  = instance_nearest(0, 0, 0);


Visual Studio has an option to output preprocessed source code which is quite helpful in determining the cause of such unexplainable errors, since they most likely are the result of unexpected macro expansions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
What am I doing wrong here? I'm trying to get my function to return a pointer to an object but I'm getting a syntax compile error :(
*** Source Snippet Removed ***
*** Source Snippet Removed ***


Some comments on that code:
- 'object' is a really bad name. It doesn't say anything about the purpose of the class and ranks right next to 'class' or 'data'. If it's an object as in a thing in your world, may I suggest "entity" or "actor" instead?
- It seems your 'object::id' is the index of an 'object' in your 'cgm::object_list' vector. That is redundant and requires pedantic bookkeeping that makes it dangerous. If you can do without it, I'd remove that field.

How about doing it like this:

object *instance_nearest(int object_id, float x, float y, int not = -1) {
std::vector<object *>::iterator nearest = object_list.end();
double nearestDistance;

for(
std::vector<object *>::iterator it = object_list.begin();
it != object_list.end();
++it
) {
bool isCandidate =
((*it)->active) &&
((*it)->object_id == object_id) &&
((*it)->id != not);

if(!isCandidate)
continue;

double distance = point_distance(x, y, (*it)->x, (*it)->y);
if(nearest == object_list.end() || distance < nearestDistance) {
nearest = it;
nearestDistance = distance;
}
} // for

if(nearest != object_list.end())
return *it;
else
return NULL; // or throw exception here
}




Share this post


Link to post
Share on other sites
"object" is the base class for 90% of my other classes,=. The actual "used" objects are stuff like "obj_ball", "proj_bullet" etc. The "object_id" is just the id that the entire class uses (obj_ball happens to be 0) as I couldn't find a way to "identify" an instance as being of a percific class :(

What would be really nice but I don't think doable would be something like
obj_ball* myball = instance_nearest(x, y);
where it then finds the nearest instance of obj_ball or
obj_target* target = instance_nearest(x, y);
where it finds the nearest instance of target.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
"object" is the base class for 90% of my other classes,=. The actual "used" objects are stuff like "obj_ball", "proj_bullet" etc. The "object_id" is just the id that the entire class uses (obj_ball happens to be 0) as I couldn't find a way to "identify" an instance as being of a percific class :(


Using Java's and .NETs the root-of-everything approach in C++ is normally not such a great idea.

You can achieve the same using RTTI in C++. For example, assuming you had the common base class 'entity' for any physical object in your game world:

entity *someEntity = instance_nearest(x, y);
ball_entity *ballEntity = dynamic_cast<ball_entity *>(someEntity);
if(ballEntity)
cout << "object is a ball" << endl;
else
cout << "object is not a ball" << endl;



But that would become a maintenance nightmare. Imagine you wanted to add a new kind of object (or entity, in my example) to your codebase. You would have to track down each and every place where your code checks the type of an object/entity and add the new entity type there if its relevant.

A better approach would be use use virtual methods and the double dispatch and visitor patterns.

Just some suggestions. As long as it works for you, it's fine ;)

-Markus-

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!