Public Group

# Division by zero

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 on other sites
Just noticed that when I take the code out of my initialize function it works fine.
The getPosition returns a reference to a vector class.

##### Share on other sites
Quote:
 Original post by Scanmaster_kJust 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_kThe getPosition returns a reference to a vector class.

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

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

##### 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 on other sites
<offtopic>

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

##### Share on other sites
Quote:
 Original post by Scanmaster_kThe 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 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.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013748
×