C ++类获取错误(SDL)

时间:2012-10-31 14:43:58

标签: c++ class sdl

  

可能重复:
  What is an undefined reference/unresolved external symbol error and how do I fix it?

嗨,我是课堂上的菜鸟,我刚开始学习它们。我需要有关此代码的帮助,因为我不知道为什么会出现此错误。

这是我的代码

的main.cpp

    #include "stdafx.h"
    #include "player.h"


SDL_Surface* screen = NULL;

void init()
{
    SDL_Init( SDL_INIT_VIDEO );

    //Set up screen
    screen = SDL_SetVideoMode( 640, 480, 32,  SDL_DOUBLEBUF |SDL_HWSURFACE |SDL_SWSURFACE);

    //SDL_BlitSurface( hello, NULL, screen, NULL );

        _putenv("SDL_VIDEO_CENTERED=1"); // Center the window 
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("SDL Animation", "SDL Animation");
    //screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_DOUBLEBUF |SDL_HWSURFACE |SDL_SWSURFACE);
    SDL_EnableKeyRepeat(0, 1600000);
    if(screen == NULL){
         printf("SDL_Init failed: %s\n", SDL_GetError());
    }


}

int _tmain(int argc, _TCHAR* argv[])
{   
    init();
    //SDL_Surface* sprite = SDL_BlitSurface(sprite,rcSprite, screen, &rcSprite);

    Player player;


    //Update Screen
    SDL_Flip( screen );

    SDL_FreeSurface(screen); 
    return 0;
}

player.h

    #ifndef PLAYER_H
#define PLAYER_H

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_TTF.h"

class Player
{
public:
    Player (){
        HP = 20;
        playerScore = 0;
        playerSpeed = 10;
        playerJump = 20;
    }
    ~Player();
    int getScore() { return playerScore;}
    void setSpeed(int speed) { int playerSpeed = speed;}
    void setJump (int jump) {playerJump = jump;}
    void movePlayer(int x ,int y);
    void runRightAnim (SDL_Rect* clip);
    void runLeftAnim (SDL_Rect* clip);
    void drawPlayer(int x, int y, SDL_Surface* source, SDL_Surface* destination);
    static SDL_Rect sprite;
private:
    static int HP;
    static int playerScore;
    static int playerSpeed;
    static int playerJump;
    static int animRate;

};

#endif

和最后一个player.cpp

    #include "stdafx.h"

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_TTF.h"

#include "player.h"

void Player::runRightAnim(SDL_Rect* clip)
{

}

void Player::runLeftAnim(SDL_Rect* clip)
{

}

void Player::drawPlayer(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
     SDL_Rect dst;
          dst.x = x;
          dst.y = y;
     SDL_BlitSurface(source,NULL,destination,&dst);
     //SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
}

,错误是

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Player::~Player(void)" (??1Player@@QAE@XZ) referenced in function _wmain
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerJump" (?playerJump@Player@@0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerSpeed" (?playerSpeed@Player@@0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerScore" (?playerScore@Player@@0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::HP" (?HP@Player@@0HA)

代码显然没有完成,但我只是想摆脱这个错误

1 个答案:

答案 0 :(得分:1)

声明并尝试使用某个函数时会发生“未解决的外部”错误,但是您没有实现它。特别是:

  

1> main.obj:错误LNK2019:未解析的外部符号“public:   __thiscall Player ::〜Player(void)“(?? 1Player @@ QAE @ XZ)在函数_wmain中引用

是因为你有Player::~Player()声明:

class Player
{
public:
  /* ... */
    ~Player();

此方法永远不会实施。

通过实施相关方法修复此错误。

编辑:此外,您的类具有已声明但从未定义的静态成员变量。你的声明在这里:

class Player
{
/* ...  */
private:
    static int HP;
    static int playerScore;
    static int playerSpeed;
    static int playerJump;
    static int animRate;

};

但是这个声明没有定义变量。你必须定义和初始化这些静态,否则你也会得到(正在获得)未解析的外部因素。

通常在cpp文件中的某处:

int Player::playerScore = 0;

顺便说一句,您正在使用static个成员变量来表示可多次实例化的类。如果你这样做:

Player a;
Player b;

实例化多个Player对象,因为成员变量被声明为static,它们将“共享”相同的playerScore等。这在查找时没有多大意义在你的代码。你可能不希望这些是static s。