Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chryzmo

Linker Error - SDL and C++

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heyas, I just tried to add some very basic collision detection to my 2D game. I don't really think I am going in the right direction with implementing this, I keep getting a linker error now. I have narrowed this linker down to a problem with to where I declare two functions. Here is the entire error: 2dEp error LNK2019: unresolved external symbol "private: int __thiscall CGame::CheckCollision(class CMap &,class CSprite &,char)" (?CheckCollision@CGame@@AAEHAAVCMap@@AAVCSprite@@D@Z) referenced in function "public: void __thiscall CGame::Loop(class CMap &,class CSprite &)" (?Loop@CGame@@QAEXAAVCMap@@AAVCSprite@@@Z) Here is the code:
// game.h


#include "sprite.h"
#include "map.h"

class CGame
{
	public:
		CGame();					// Constructor

		~CGame();					// Destructor

		void Launch();
		void Loop(CMap &Map, CSprite &Sprite);

	private:
            //  This is what causes the error, if i comment it out everything compiles fine.

		int CheckCollision(CMap &Map, CSprite &Sprite, char direction);
};

// game.cpp

// Game Loop

void CGame::Loop(CMap &Map, CSprite &Sprite)
{
	// This will be the game loop

	Map.LoadTiles();
	Map.LoadMap();
	Sprite.LoadSprite();
	Map.DrawBG();
	Map.DrawSprite(Sprite.ShowSrcRect(), Sprite.ShowDstRect(), Sprite.SpriteSurface);

	SDL_Event l_Event;
	bool l_GameBool = true;
	char l_dir = NULL;

	while (l_GameBool == true)
	{
		if (SDL_PollEvent(&l_Event) == 1)
		{
			switch(l_Event.type)
			{
				case SDL_QUIT:
					l_GameBool = false;
					break;
				case SDL_KEYDOWN:
					switch(l_Event.key.keysym.sym)
					{
					case SDLK_UP:
						l_dir = 'u';
						CheckCollision(Map, Sprite, l_dir);
						break;
					case SDLK_DOWN:
						l_dir = 'd';
						CheckCollision(Map, Sprite, l_dir);
						break;
					case SDLK_RIGHT:
						l_dir = 'r';
						CheckCollision(Map, Sprite, l_dir);
						break;
					case SDLK_LEFT:
						l_dir = 'l';
						CheckCollision(Map, Sprite, l_dir);
						break;
					case SDLK_ESCAPE:
						l_GameBool = false;
						break;
					}
					break;
			}
			Map.DrawBG();
			Map.DrawSprite(Sprite.ShowSrcRect(), Sprite.ShowDstRect(), Sprite.SpriteSurface);
			Map.Update();
		}
	}
}
// Check Collision Detection Function

int CheckCollision(CMap &Map, CSprite &Sprite, char direction)
{
	int liReturn = 0;
	
	int liSpriteXTile = Sprite.ShowXPos() / 30;
	int liSpriteYTile = Sprite.ShowYPos() / 30;

	switch(direction)
	{
	case 'u':
		if (Map.TileStatus(liSpriteXTile+1, liSpriteYTile) == 0)
		{
			Sprite.StepUp();
		} else {
			liReturn = 0;
		}
		break;
	case 'd':
		if (Map.TileStatus(liSpriteXTile-1, liSpriteYTile) == 0)
		{
			Sprite.StepDown();
		} else {
			liReturn = 0;
		}
		break;
	case 'r':
		if (Map.TileStatus(liSpriteXTile, liSpriteYTile+1) == 0)
		{
			Sprite.StepRight();
		} else {
			liReturn = 0;
		}
		break;
	case 'l':
		if (Map.TileStatus(liSpriteXTile, liSpriteYTile-1) == 0)
		{
			Sprite.StepLeft();
		} else {
			liReturn = 0;
		}
		break;
	default:
		fprintf(stderr, "\nCollision Detection error: Invalid direction parameter\n");
		break;
	}
	return liReturn;
}
I know that the Step functions work, I had those moving the sprite around before I tried added the Collision detection. Could someone help me with this linker error? And perhaps some tips on how I am structuring the code? I personally believe this isn't really the most efficient way to be structuring my code. It works(or did before I made this recent change), but I can see it getting fairly messy soon. Particularly with the classes not being completely independent of each other. I am still very new with SDL and fairly new with classes. Any tips would be very appreciated. Thanks, Chris edit: The error would probably have helped... [edited by - Chryzmo on May 20, 2004 8:41:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
Change
int CheckCollision(CMap &Map, CSprite &Sprite, char direction)
to
int CGame::CheckCollision(CMap &Map, CSprite &Sprite, char direction)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!