SDL C ++类函数错误

时间:2013-07-19 00:25:20

标签: c++ class sdl

来自main.cpp的代码片段

playerEntity::handle()
{
    if( event.type == SDL_KEYDOWN )
    {
            switch( event.key.keysym.sym )
            {
                    case SDLK_q:
                            running = false;
                            paused = true;
                            break;
                    case SDLK_ESCAPE:
                            paused = !paused;
                            break;
            }
    }

    if( keystate[SDLK_UP] )
    {
            if( isJumping == false && isFreeFalling == false )
            {
                    isJumping = true;
            }
    }
    if( keystate[SDLK_LEFT] )  player.hitbox.x--;
    if( keystate[SDLK_RIGHT] ) player.hitbox.x++;
    if( player.hitbox.x < 0 ) {player.hitbox.x = 0;}
    else if( player.hitbox.x > screen.WIDTH - player.hitbox.w ) {player.hitbox.x = screen.WIDTH - player.hitbox.w;}
    if( player.hitbox.y < 0 ) {player.hitbox.y = 0;}
    else if( player.hitbox.y > screen.HEIGHT - player.hitbox.h ) {player.hitbox.y = screen.HEIGHT - player.hitbox.h;}
}

在头文件中定义playerEntity的地方:

#ifndef PLAYERENTITY_H
#define PLAYERENTITY_H

class playerEntity
{
    private:
            int jumpHeight;
            int jump;
            bool isJumping;
            bool isFalling;
            bool isFreeFalling;
            SDL_Event event;
            Uint8 *keystate;
    public:
            playerEntity();
            void jump();
            void handle();
            void fall();
            int health;
            int damage;
            SDL_Rect hitbox;
            bool evolved;
};

#endif

当我尝试编译时,我得到错误: ISO c ++禁止声明'句柄'没有类型[-fpermissive] 'int playerEntity :: handle()'的原型与类'playerEntity'中的任何一个都不匹配 错误:候选者是:void playerEntity :: handle()。 我仍然是头文件和类的新手,我该如何修复错误?

2 个答案:

答案 0 :(得分:0)

你应该替换

playerEntity::handle()

void playerEntity::handle()

答案 1 :(得分:0)

void playerEntity::handle()

C ++要求在函数定义中提及返回类型(在本例中为非类型void) - 一种重要的类型安全措施。

顺便说一下,您应该将playerEntity::handle()的定义从main.cpp移到新文件playerEntity.cpp。其他文件也是可能的,但很少有优秀的程序员会在main.cpp中留下这个定义。不幸的是,实际上幸运的是,这将让你经历几个小时的学习单独编译和链接的紧迫必要的痛苦。

祝你好运。