2013-07-19 62 views
1

从main.cpp中的我的代码片段的SDL C++类功能错误

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'中的任何类型 error:candidate is:void playerEntity :: handle )。 我对头文件和类还不熟悉,我该如何解决这些错误?

回答

0

你应该

void playerEntity::handle() 
0

void playerEntity::handle() 

C++需要的返回类型(在这种情况下是无类型void)是在提及取代

playerEntity::handle() 

函数的定义—一个重要的类型安全措施。

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

祝你好运。