Thread: linker 2019 issues

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    Exclamation linker 2019 issues

    Hi,

    I have been playing around with SDL recently and also trying to learn more about classes and pointers too. I have a problem with code I have implented from this site http://www.aaroncox.net/tutorials/2d...dlclasses.html

    Player.obj : error LNK2019: unresolved external symbol "public: struct SDL_Surface * __thiscall SDL_Graphics::Load_Image(char *,int,int,int)" (?Load_Image@SDL_Graphics@@QAEPAUSDL_Surface@@PADH HH@Z) referenced in function "public: __thiscall Player::Player(class SDL_Graphics *,int,int,int,int,int,int,int,char *,float,float,float)" (??0Player@@QAE@PAVSDL_Graphics@@HHHHHHHPADMMM@Z)

    is the error.

    here is some of the code....

    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    
    #include "sdl.h"
    #include "sdlinit.h"
    
    
    class Player
    {
    public:
    	  Player(SDL_Graphics* screen,
                 int imageX, int imageY,
                 int width, int height,
                 int transparentR, int transparentG, int transparentB,
                 char* FileName,
                 float x, float y,
                 float maxSpeed);
    
       ~Player();
    	void MoveLeft();
    	void MoveRight();
    	void MoveUp();
    	void MoveDown(); // these 4 adjust the characters position based on the speed in pixels
    	void StopMoving();
    	void Update(float deltatime); // this adjusts the speed to make it work to pixels per second.
    
    private:
    	SDL_Graphics* screen;
    	SDL_Surface* playerimage;
    	float maxspeed ; // speed in pixels per second.
    	float currentspeedx;
    	float currentspeedy;
    	float x;
    	float y; //  position on screen
    	float Width;
    	float Height;
    	float imagex;
    	float imagey;
    
    };
    #endif
    
    player.cpp
    #include "Player.h"
    
    
    Player::Player(SDL_Graphics* screen,int imageX, int imageY,int width, int height,
    				int transparentR, int transparentG, int transparentB,
    				char* FileName,float x, float y,float maxSpeed)
    								: screen(screen),
                                      imagex(imageX), imagey(imageY),
                                      Width(width), Height(height),
                                      x(x), y(y),
                                      maxspeed(maxSpeed),
                                      currentspeedx(0.0f)
    {
    	playerimage = screen->Load_Image(FileName, transparentR, transparentG, transparentB);
    }
    Player::~Player()
       {
    	   screen->CloseImage(playerimage);
       }
    
    void Player::MoveLeft()
    	{
    	}
    void Player::MoveRight()
    	{
    	}
    
    void Player::MoveUp()
    {
    }
    void Player::MoveDown() // these 4 adjust the characters position based on the speed in pixels
    {
    }
    
    void Player::StopMoving()
    {
    }
    
    
    void Player::Update(float deltatime) // this adjusts the speed to make it work to pixels per second.
    	{
    
    	}
    sdlinit.h
    #ifndef SDLINIT_H
    #define SDLINIT_H
    
    #include "sdl.h"
    #include "SDL_TTF.h"
    #include "SDL_Image.h"
    
    class SDL_Graphics
    {
    public:
    	SDL_Graphics(int width, int height, char* windowtitle, int Red , int Green , int Blue );
    	~SDL_Graphics();
    	
    	SDL_Surface* Load_Image(char* filename, int Red, int Green, int Blue);
    	void CloseImage(SDL_Surface* image);
    	void BackgroundColour(int Red,int Green,int Blue);
    	void Drawtext(int xpos, int ypos, char* message, int Red, int Green, int Blue, int fontsize);
    	void DrawSprite(SDL_Surface* imageSurface, int srcX, int srcY, int dstX, int dstY, int width, int height);
    	void Flip();
    	void BeginScene();
    	void EndScene();
    
    private:
    	SDL_Surface* screen;
    	int BackRed;
    	int BackGreen;
    	int BackBlue;
    };
    #endif
    sdlinit.cpp
    #include "sdlinit.h"
    
    SDL_Graphics::SDL_Graphics(int width, int height, char* windowtitle, int Red, int Green, int Blue)
    {
    	SDL_Init( SDL_INIT_VIDEO );
    	TTF_Init();
    	screen = SDL_SetVideoMode( width, height,0,SDL_HWSURFACE | SDL_DOUBLEBUF);
    	SDL_WM_SetCaption( windowtitle, 0 );
    	BeginScene();
    	BackRed = Red; BackGreen = Green; BackBlue = Blue;
    }
    void SDL_Graphics::BeginScene()
    {
    		SDL_FillRect(screen,
    		NULL,
    		SDL_MapRGB(screen->format,BackRed,BackGreen,BackBlue));
    }
    void SDL_Graphics::EndScene()
    {
    	SDL_Flip(screen);
    }
    SDL_Surface* Load_Image(char* filename, int red, int green, int blue)
    {
    	SDL_Surface* tempimage = NULL;
    	tempimage = IMG_Load(filename);
    	if (tempimage == NULL)
    		return NULL;
    	SDL_SetColorKey(tempimage,SDL_SRCCOLORKEY,SDL_MapRGB(tempimage->format, red, green, blue)); 
    	return tempimage;
    }
    
    void CloseImage(SDL_Surface* image)
    {
    	SDL_FreeSurface(image);
    }
    
    void SDL_Graphics::BackgroundColour(int Red = 0 , int Green = 0, int Blue = 0)
    {
    	BackRed = Red; BackGreen = Green; BackBlue = Blue;
    }
    SDL_Graphics::~SDL_Graphics()
    {
    	SDL_Quit();
    	TTF_Quit();
    }
    void SDL_Graphics::Drawtext(int xpos, int ypos, char* message, int Red, int Green, int Blue, int fontsize)
    {
    	TTF_Font* font = TTF_OpenFont("c:\\windows\\fonts\\arial.ttf", fontsize);
    	SDL_Color Colour = {Red,Green,Blue};
    	if (font == NULL)
    		return;
    
    	SDL_Surface* textsurface;
    	textsurface = TTF_RenderText_Blended(font, message, Colour);
    	SDL_Rect textlocation = {xpos,ypos,0,0};
    	SDL_BlitSurface(textsurface,NULL,screen,&textlocation);
    	SDL_FreeSurface(textsurface);
    	TTF_CloseFont(font);
    }
    
    void SDL_Graphics::Flip()
    {
    	SDL_Flip(screen);
    }
    
    void SDL_Graphics::DrawSprite(SDL_Surface* imageSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
    {
       SDL_Rect srcRect;
       srcRect.x = srcX;
       srcRect.y = srcY;
       srcRect.w = width;
       srcRect.h = height;
    
       SDL_Rect dstRect;
       dstRect.x = dstX;
       dstRect.y = dstY;
       dstRect.w = width;
       dstRect.h = height;
    
       SDL_BlitSurface(imageSurface, &srcRect, screen, &dstRect);
    }
    the problems lies in the constructor for the Player class where it calls the SDL_Graphics classes function LoadImage().

    It also has a problem with CloseImage.

    I think it is something I have done wrong with my classes.

    I've been googling and am unable to come up with a solution to my problem so far. which makes me think its something to do with my classes. if I comment out the calls to the Loadimage and close image in my player constructor and destructor it links ok.

    any help or advice would be greatly appreciated.
    werdy666!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is sdlinit.cpp in your project?
    You need to compile all your .cpp files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    SDL_Surface* Load_Image(char* filename, int red, int green, int blue);
    void CloseImage(SDL_Surface* image);
    Note how they are defined in the global namespace and not as part of the class (missing SDL_Graphics::).
    Last edited by Elysia; 02-24-2009 at 12:52 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    33

    Smile

    i knew it would be something simple that i had done wrong!

    thanks!
    werdy666!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  2. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 01:25 AM
  3. DllMain implementation
    By George2 in forum C++ Programming
    Replies: 48
    Last Post: 02-15-2008, 03:52 AM
  4. Linker issues....
    By verbity in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2007, 02:08 AM
  5. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM