• Advertisement
Sign in to follow this  

Division by zero

This topic is 4182 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 got this code:
CCircle *circle = new CCircle(10, 15.0);
circle->getPosition()(rand() % mWindow.getWidth(), rand() % mWindow.getHeight(), 0.0);
circle->getVelocity()(rand() % 10 + 1, rand() % 10 + 1, 0.0);
mCircles.push_back(circle);
When I only create one circle it works fine but whenever I create several I get a division by zero run-time error. Can some one help me why that is? Thanks Scanmaster_K

Share this post


Link to post
Share on other sites
Advertisement
I don't see any division in that code. Run your program through a debugger and post the actual line of code where the error happens.

-me

Share this post


Link to post
Share on other sites
Unless circle->getPosition() and circle->getVelocity() return functors, I'm pretty certain that code is invalid. First of all, are you sure that that is the exact code? Secondly, are you sure that's where the divide by zero errors are coming from?

Share this post


Link to post
Share on other sites
Quote:
Original post by Scanmaster_k
Just noticed that when I take the code oit of the constructor it works fine.

Have you used a debugger to find out exactly where the errors are occuring, yet?

Quote:
Original post by Scanmaster_k
The getPosition returns a reference to a vector class.

And the vector class has the () operator overloaded? Why?

Share this post


Link to post
Share on other sites
your getWidth() or getHeight() are likely returning 0. modding by 0 is a divide by 0 error.

Share this post


Link to post
Share on other sites
The debugger gives the error message at this line:

circle->getPosition()(rand() % mWindow.getWidth(), rand() % mWindow.getHeight(), 0.0);


getWidht and height gives valid return values.

I use the () of my vector class to set the values in the array which hold the data like vec(index, value) or vec(0, 1, 2, 3)

Share this post


Link to post
Share on other sites
<offtopic>

I find it icky to use a get function to set values, even if indirectly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scanmaster_k
The debugger gives the error message at this line:

circle->getPosition()(rand() % mWindow.getWidth(), rand() % mWindow.getHeight(), 0.0);


getWidht and height gives valid return values.

I use the () of my vector class to set the values in the array which hold the data like vec(index, value) or vec(0, 1, 2, 3)


So you are using it to set the position? A setPosition method would be a lot easier to read.

EDIT: Additionally, is that as deep as the error goes? It doesn't occur in the function called?

Share this post


Link to post
Share on other sites
Yeah I actually agree but this is an old class I did and I was lazy and all of those kinds of excuses.

Don't think so, as it works fine when I take it out of my initialize function, it's just weird.

Share this post


Link to post
Share on other sites
Well, either mWindow.getWidth() or mWindow.getHeight() is returning zero. Remember % is a division-related operation, so it has the same problems with applying a modulus by zero.

[edit]Unless by valid values, you meant non-zero.[/edit]

Share this post


Link to post
Share on other sites
That is correct, when I kept the code in the initialize function the window initialization returned false, just don't see why as it returned true when I removed the circle creating code.

Thanks for your help guys.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scanmaster_k
Yeah I actually agree but this is an old class I did and I was lazy and all of those kinds of excuses.


I suspect "confused" is closer to the mark than "lazy". And anyway, you know better now, so get to it! :P

(Seriously: when you fix your real *problems*, your questions often answer themselves.)

Share this post


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

  • Advertisement