Sign in to follow this  

Need help with error

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

int width=800,height=600,objects=0,maxobjects=OBJECT_CLUSTER;
float theta=0.0,ratio=0.75,elapsed_time,last_time,t;

typedef struct
{
  float x,y,z;
}
VERTEX;

typedef struct
{
  int v, max_v;
  float x,y,z;
  VERTEX *vertex;
}
OBJECT;
OBJECT *object;

void Init3D(void)
{
  object=(OBJECT*)malloc(OBJECT_CLUSTER*sizeof(OBJECT));
}

void createObject(float new_x,float new_y,float new_z)
{
  ++objects;
  if(objects>maxobjects)
  {
    maxobjects+=OBJECT_CLUSTER;
    object=realloc(object,maxobjects*sizeof(OBJECT));
  }
  object[objects-1].v=0;
  object[objects-1].max_v=VERTEX_CLUSTER;
  object[objects-1].x=new_x;
  object[objects-1].y=new_y;
  object[objects-1].z=new_z;
  object[objects-1].vertex=(VERTEX*)malloc(VERTEX_CLUSTER*sizeof(VERTEX));
}

void createVertex(OBJECT *obj,float new_x,float new_y,float new_z)
{
  ++obj->v;
  if(obj->v>obj->max_v)
  {
    obj->max_v+=VERTEX_CLUSTER;
    obj->vertex=realloc(obj->vertex,obj->max_v*sizeof(OBJECT));
  }
  obj->vertex[obj->v-1].x=new_x;
  obj->vertex[obj->v-1].y=new_y;
  obj->vertex[obj->v-1].z=new_z;
}

int main()
{
  Init3D();
  createObject(1,2,3);
  createVertex(object[0],3,3,3);
}
...

I get an error "incompatible type for argument 1 of 'createVertex'" What type am i supposed to use then? :/

Share this post


Link to post
Share on other sites
I think that by indexing the object array, you are, in essence, dereferencing it. You might try passing just object, or passing &object[0], which I think is clearer.

Share this post


Link to post
Share on other sites
You need to send a pointer to an OBJECT It appears as you are sending an Actual object. If you wish to operate on the ith object in your array you'd do:

createVertex(&object[i],3,3,3);


or maybe:

createVertex((object + i),3,3,3);


Where i is the object you wish to operate on(0 in the case of the last line)

I think that's correct, hope that helps.

EDIT: grr, too slow

Share this post


Link to post
Share on other sites
void createVertex(OBJECT obj,float new_x,float new_y,float new_z)
{
++obj.v;
if(obj.v>obj.max_v)
{
obj.max_v+=VERTEX_CLUSTER;
obj.vertex=realloc(obj.vertex,obj.max_v*sizeof(OBJECT));
}
obj.vertex[obj.v-1].x=new_x;
obj.vertex[obj.v-1].y=new_y;
obj.vertex[obj.v-1].z=new_z;
}
...
createVertex(object[0],3,3,3);
...



Is there anything logically wrong with this? Looks like it works but i dont want a risk of crashing/etc.

Edit: i've tried using it and it doesnt work. The program cant read the numbers from the dynamic array correctly if i tried the above so i went back to the last one with *, ->, & stuff.

[Edited by - biscuitz on June 16, 2006 2:45:18 PM]

Share this post


Link to post
Share on other sites
I can't see anything immediately wrong with it, but I'm at work, so I'll have to look more later. I do find it a little worrisome that you're using C. Is there a reason you don't want to use C++? I abso-tively love std::vector and std::list and such for handling situations like these.

Share this post


Link to post
Share on other sites
It's ok syntactically, but the semantics are not right: C and C++ are pass-by-value (unless you explicitly request otherwise), so you pass a *copy* of the OBJECT from the array, modify the copy, and then at the end of the function the copy is thrown away (because it's a local variable and hasn't been assigned anywhere) and the global array is untouched.

The other posters have it correct in terms of how to set up a pointer. However, if you used C++, you could pass by reference instead: have the function accept "OBJECT&" rather than "OBJECT", and everything else stays the same. Basically this generates pointer-like stuff for you "under the hood", but is safer and cleaner (and you really shouldn't think of it in terms of the implementation details: see here for a full rundown on references).

Share this post


Link to post
Share on other sites
what do you mean by "Correctly". if your getting messed up number , the problem
might be with the float vars. floats have a little strange behavoir ,for example
if you write 1/3 the result is 0 . if that seems to be the problem , i advice that you google it.

Share this post


Link to post
Share on other sites
Zahlman i dont understand what you're saying :/

Code Fusion I tried to access the data and it kept giving me 0 no matter what i set the numbers to (as far as i could figure). Oh wait... i remember, it wouldn't read the y/z values I think, because i found it would read one of the values but not the others.

I tried to learn C++ templates but i got lost. And I already new more about C memory functions than the c++ templates so i decided to keep going C rather than trying to switch to something new when i'm so close to finishing the code.

Share this post


Link to post
Share on other sites

This topic is 4203 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this