简体   繁体   English

SDL C ++类函数错误

[英]SDL C++ Class Function Error

An snippet of my code from main.cpp 来自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;}
}

Where playerEntity is defined in a header file: 在头文件中定义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

And when I try to compile I get the errors: ISO c++ forbids declaration of 'handle' with no type [-fpermissive] prototype for 'int playerEntity::handle()' does not match any in class 'playerEntity' error: candidate is: void playerEntity::handle(). 当我尝试编译时,出现错误:ISO c ++禁止声明“ handle”,而没有类型“ -fpermissive”原型,用于“ int playerEntity :: handle()”,与类“ playerEntity”中的任何错误都不匹配: :void playerEntity :: handle()。 I am still new to header files and classes, how do I fix the errors? 我还是头文件和类的新手,如何修复错误?

you should replace 你应该更换

playerEntity::handle()

with

void playerEntity::handle()

Write

void playerEntity::handle()

C++ requires the return type (which in this case is the nontype void ) to be mentioned in the function's definition—an important type-safety measure. C ++要求在函数的定义中提及返回类型(在这种情况下为非类型void ),这是一种重要的类型安全措施。

By the way, you should probably move the definition of playerEntity::handle() from main.cpp to a new file, playerEntity.cpp . 顺便说一句,您应该将playerEntity::handle()的定义从main.cpp移到新文件playerEntity.cpp Other files are possible, too, but few good programmers would leave this definition in main.cpp . 其他文件也是可能的,但是很少有优秀的程序员会在main.cpp保留此定义。 Unfortunately—well, actually fortunately—this will put you through several hours of the urgently necessary pain of learning separate compilation and linking. 不幸的是,实际上幸运的是,这将使您度过数小时的学习单独的编译和链接的迫切痛苦

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM