Strange error with default constructor (C ++)

First of all, I would 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 something else, tell me.

So. I have this piece of code: (I am using SFML for vector and CircleShape)

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

}

And here is the header (# is included in the specified file):

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();

};

When I create a ball with

Ball ball;

( , SFML ), . , loc.x loc.y , , , , fillcolor .. . std:: cout , loc.x loc.y , , . ,

Ball ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);

Ball ball(400, 300);

, . . - , .

, OS X 10.8 Xcode 4.5.2 SFML RC2.0, .

,

Matt

+5
7

++ ++ 11

. - :

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.

   init((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);

}

Ball::Ball(float x, float y) {

    init(x,y);

}

Ball::init(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);

}
+2

( ) ++ 11. ++ 11, :

Ball::Ball()
 : Ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS,
        (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS)
{ }

Pre-++ 11, , , .

Ball::Ball() {
  init((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS,
       (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);
}

Ball::Ball(float x, float y) {
  init(x, y);
}

void Ball::init(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);
}
+6

++ , , , , , , .

.

+3

++, ++ 11, , , .

:

+2

I would suggest using biphasic initialization instead of creating a chain of constructors, which would mean creating a function init()that you call in your default constructor.

+1
source

Other answers provide a syntactically correct way to do this.

I would do something semantically correct for you to call it like this:

Ball ball = Ball::createRandom();

You implement createRandomas a function static Ball:

class Ball {
public:
    //...
    static Ball createRandom();
};

Implemented as:

int randomisePosition(int position) {
    return (rand() % (position - (2 * BALL_RADIUS))) + BALL_RADIUS;
}

Ball Ball::createRandom() {
    return Ball(randomisePosition(WINDOW_X),
                randomisePosition(WINDOW_Y));
}
+1
source

You must make the init () method and call it in both constructors.

Ball::Ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);

creates a temp ball object and destroys it immediately

0
source

All Articles