-2

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

Hi I am noob with classes and I just started learning them. I need help with this code as I have no idea why I get this error.

Here is my Code

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

and the last one 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);
}

and the error is

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)

The code is obviously not finished but I just want to get rid of this error

Community
  • 1
  • 1
Laggy
  • 58
  • 1
  • 10

1 Answers1

1

"unresolved external" errors occur when you declare and attempt to use a function, but you don't implement it. In particular:

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Player::~Player(void)" (??1Player@@QAE@XZ) referenced in function _wmain

Is because though you have a declaration for Player::~Player():

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

This method is never implemented.

Fix this error by implementing the methods in question.

Edit: Additionally, your class has static member variables which are declared but never defined. Your declaration is here:

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

};

But this declaration does not define the variables. You must define and initialize these statics, else you will get (are getting) unresolved externals here as well.

Generally somewhere in the cpp file:

int Player::playerScore = 0;

As an aside, you are using static member variables for a class which appears to be instantiatable multiple times. If you do this:

Player a;
Player b;

to instantiate multiple Player objects, because the member variables are declared static they will each "share" the same playerScore etc. This doesn't make a lot of sense when looking at your code. You probably won't want these to be statics.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • and the static members need to exist somewhere, too. – kratenko Oct 31 '12 at 14:50
  • ohh sweet it worked thanks so much :D, I have only started using classes, just another noob question what is "static" used for any way – Laggy Oct 31 '12 at 14:56
  • I have my doubts about the static members. Mostly likely they shouldn't be static at all. I think the OP has just got this wrong (as he's says he's new at this). – john Oct 31 '12 at 14:56
  • @Laggy, You added static without knowing what it is for? That's confidence I suppose. Having briefly looked at your code I think you should delete the static. – john Oct 31 '12 at 14:57
  • Thanks for your help guys ,I should read some info before using these :D – Laggy Oct 31 '12 at 15:01