Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange bug with default constructor (C++)
    text
    copied!<p>First of all, I'd like to apologize in advance if the answer is obvious; I am very new to C++ and my first language is Java. I am also new to Stack Overflow, so if something is wrong with my question or you need anything else, please tell me.</p> <p>So. I have this piece of code here: (I am using SFML for the vector and the CircleShape)</p> <pre><code>Ball::Ball() { // This ugly thing calls the full constructor with a random x and y position // in such a way the the entire ball is inside the screen. Ball::Ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS); } Ball::Ball(float x, float y) { loc.x = x; loc.y = y; ball.setPosition(loc.x - BALL_RADIUS, loc.y - BALL_RADIUS); ball.setRadius(BALL_RADIUS); ball.setFillColor(sf::Color::Red); ball.setOutlineColor(sf::Color::Black); ball.setOutlineThickness(1); } </code></pre> <p>And here is the header (#included into the above file):</p> <pre><code>class Ball { private: sf::CircleShape ball; sf::Vector2f loc; sf::Vector2f vel; sf::Vector2f acc; void update(); void bounce(); void draw(); public: Ball(); Ball(float x, float y); void run(); }; </code></pre> <p>When I create the ball with</p> <pre><code>Ball ball; </code></pre> <p>(and yes, all of the SFML rendering stuff works), it never shows. A bit of investigation shows that its loc.x and loc.y variables are not set, and probably, neither are the radius, fillcolor, etc of the ball object. If I print the values of these with std::cout <em>inside</em> the constructor, loc.x and loc.y and all the others <em>are</em> set, so I assume that they get unset somewhere after the constructor. What is strange is that if I create the ball with </p> <pre><code>Ball ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS); </code></pre> <p>or even</p> <pre><code>Ball ball(400, 300); </code></pre> <p>everything works perfectly, and the ball shows up on-screen. I am really stumped guys. If anyone could help me, that would be great.</p> <p>BTW, I am running OS X 10.8 with Xcode 4.5.2, and using SFML RC2.0, if that makes any difference.</p> <p>Thanks,</p> <p>Matt</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload