Search code examples
csdldeclarationsdl-2rect

SDL_Rect declaration with values not working


I'm studying a code that contains:

...
SDL_Rect rect(0, 0, 100, 50);
...

But when I do it, i get the following error:

error: expected declaration specifiers or ‘...’ before numeric constant
   49 |   SDL_Rect rect(0, 0, 100, 50);

Can someone tell me why?


Solution

  • If you're compiling as C, this is invalid. There are no constructors in C. The compiler is interpreting the first part of your code as a forward declaration, and the latter part confuses it.

    Just define the SDL_Rect as such

    SDL_Rect rect;
    
    rect.x = 0;
    rect.y = 0;
    rect.w = 100;
    rect.h = 50;
    

    Here is the libsdl reference: https://wiki.libsdl.org/SDL_Rect

    Although since you're studying code, you probably want to just compile this as C++ as it appears that is what it was written in.