Sign in to follow this  
deavik

Trouble with scope

Recommended Posts

I have a class camera_t with a public member to store the last tick time, appropriately named last_tick. No I am calling a function with a camera_t class passed as reference to change this last tick time. It isn't happenning. void snap_car (camera &cam, car_instance car) { // change position and other stuff printf("lasttick: %d\n", cam.last_tick); cam.last_tick = SDL_GetTicks(); printf("lasttick: %d\n", cam.last_tick); // do other stuff } Now the thing is other member variables are modified properly by this function. The output I get is like this: lasttick before: 294 lasttick after: 295 lasttick before: 294 lasttick after: 353 lasttick before: 294 lasttick after: 363 lasttick before: 294 lasttick after: 380 lasttick before: 294 .... So the change occurs in the function (as it should) but the next time the function is called, I see the variable hasn't changed at all! Any ideas?

Share this post


Link to post
Share on other sites
I'd expect if the object was re-created, last_tick would be reset to zero, but it does sound like a scope problem. It doesn't look like the function is wrong as such; I think it'd help if you posted the code that calls the function. Try putting printf or cout calls in the constructor and destructor, which output the value of last_tick.

In other words, I concur. We need the appropriate source codex.

PS: (Plus, the code you have posted wouldn't produce the output you've given, so either you've posted code that only looks like the actual code (which means we can't spot silly errors) or you're calling a different function than you think you are.)

Oh, and a minor issue, but it's bugging me, why isn't snap_car a member function? [smile]

Share this post


Link to post
Share on other sites
Mr. Evil, don't worry it's the same function, I just added the "before" and "after" to make the output more readable than having "lasttick" on each line. Why it isn't a member function is a long story. I was having linker errors for the member functions (something about having previously declared them as int functions and they cannot be overloaded yada yada). Go ahead, laugh at me all you want, but I took the easy way out and took them out of the class.

Getting to the issue, both of you guys are absolutely right, the destructor is being called after each cycle. After I deduced that I could fix it easily. The function calling it was:

void glDraw(car_instance car, camera cam)
{
snap_car(cam, car);
find_angles(car, _front, _theta, _x, _y, _z);

// gl stuff
}

All I had to do was make sure the camera was passed as reference:

void glDraw(car_instance car, camera &cam)
{
snap_car(cam, car);
find_angles(car, _front, _theta, _x, _y, _z);

// gl stuff
}

and it works! Thanks you guys for the help!

Share this post


Link to post
Share on other sites

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