C++令牌之前'('预期的构造函数、析构函数或类型转换

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

本文关键字:构造函数 类型转换 析构函数 令牌 C++      更新时间:2023-10-16

我知道你可能会认为这篇文章是另一篇重复文章,但事实并非如此。它不是重复的,因为我的代码与其他代码不同,而且没有人的帖子(我见过)解决了我的问题。

这是我的代码:

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

错误在DrawPlayer(20,20);

您没有从任何函数中调用DrawPlayer()

不能在任何方法之外调用DrawPlayer。调用必须在类方法或全局方法内部。

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

DrawPlayer需要在main或其他函数中。

当不在函数体中时,如何调用函数?