1

我的代码片段来自 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++ forbids declaration of 'handle' with no type [-fpermissive] prototype for 'int playerEntity::handle()' does not match any in class 'playerEntity' 错误:候选人是: 无效 playerEntity::handle()。我还是头文件和类的新手,如何修复错误?

4

2 回答 2

0

你应该更换

playerEntity::handle()

void playerEntity::handle()
于 2013-07-19T00:27:38.550 回答
0

void playerEntity::handle()

C++ 要求在函数定义中提及返回类型(在本例中为 nontype void)——这是一个重要的类型安全措施。

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

祝你好运。

于 2013-07-19T00:34:28.590 回答