Jack Wilsdon
Jack Wilsdon

Reputation: 7025

C++ expected constructor, destructor, or type conversion before '(' token

I know you may regard this post as another duplicate, while it's not. It's not a duplicate because my code is different than others, and nobody else's post (that I have seen) has solved my problem.

Here is my code:

#include "sprite.h"

SDL_Surface * SPRITE::screen;
int player;
void DrawPlayer(int x, int y) {
    SDL_Rect rect = {x,y,20,20};
    SDL_FillRect(SPRITE::screen, &rect, 0x00CC00);
}
DrawPlayer(20,20);

The error is on the line DrawPlayer(20,20);

Upvotes: 0

Views: 1223

Answers (5)

01100110
01100110

Reputation: 2344

Try this:

#include "sprite.h"

SDL_Surface * SPRITE::screen;
int player;
void DrawPlayer(int x, int y) {
    SDL_Rect rect = {x,y,20,20};
    SDL_FillRect(SPRITE::screen, &rect, 0x00CC00);
}

int main()
{
    DrawPlayer(20,20);
    return 0;
}

Upvotes: 2

James
James

Reputation: 9278

How can you call a function when not in a function body?

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

DrawPlayer needs to be in main or another function.

Upvotes: 1

spencercw
spencercw

Reputation: 3358

You are not calling DrawPlayer() from within any function.

Upvotes: 2

Dervall
Dervall

Reputation: 5744

You cannot have your call to DrawPlayer outside of any method. The call must be inside a class method or a global method.

Upvotes: 2

Related Questions