Sign in to follow this  
Scanmaster_k

Division by zero

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
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
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
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
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
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

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