1

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)

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Arsen Simonean
  • 362
  • 2
  • 17
  • 2
    You've stumbled upon the [vexing parse](http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work) – eerorika May 12 '14 at 22:17
  • 3
    @user2079303: No he hasn't. The vexing parse occurs when there is syntax that the user means to be an object creation, but the syntax is ambiguous with a function declaration, and the compiler parses it as the latter. This is just incorrect syntax altogether. Or at least, it is syntax that is not allowed in this particular context (the body of a class). – Benjamin Lindley May 12 '14 at 23:10
  • 1
    @BenjaminLindley: Well, he does mean his code to be an object creation and the compiler does interpret it as a function declaration (although fails because of the arguments). Even if this isn't the canonical *vexing parse*, I'd argue that this is just as vexing because the compiler error is related to incorrect function declaration rather than incorrect member initialization. I'd be willing to bet that c++11 didn't add non-static member initialization with this particular syntax exactly because it's always ambiguous with function declaration. – eerorika May 13 '14 at 08:07

1 Answers1

1

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.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274