Question

Hi im getting error when trying this code:

    #include <SFML/Window.hpp>
    #include <SFML/Graphics.hpp>

    const float ballVelocity = 8.f;

    struct Ball
    {
        Vector2f velocity(-ballVelocity, -ballVelocity);
    };

I'm using SFML and doing it by a tutorial, and it is supposed to work, but Visual Studio sais that in this line:

Vector2f velocity(-ballVelocity, -ballVelocity);

Expected a type specifier.

(EDIT: to be clear Im trying to create object of Vector2)

Was it helpful?

Solution

If you're trying to use non-static data member initialization, you cannot do it like that. You need to either use brace initialization, or equal(=) initialization. Either of these should work:

struct Ball
{
    Vector2f velocity {-ballVelocity, -ballVelocity};
};

Or this:

struct Ball
{
    Vector2f velocity = Vector2f(-ballVelocity, -ballVelocity);
};

Although, if I'm not mistaken, the components of SFML exist in the sf namespace, so any references to Vector2f actually need to be qualified as sf::Vector2f.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top