Sign in to follow this  
MerlinCoder

Hello, and function pointer struct issue

Recommended Posts

Hi there, I've spent roughly half an hour searching the forum for help on this so sorry if a similar thing has already been asked but I couldn't find it, or at least something simple enough for me to understand. I'm trying to set up a stack of structs that hold function pointers, but I cannot get my code to compile. I get the following error: error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: struct states const & __thiscall std::_Deque_const_iterator<struct states,class std::allocator<struct states>,1>::operator*(void)const " (??D?$_Deque_const_iterator@Ustates@@V?$allocator@Ustates@@@std@@$00@std@@QBEABUstates@@XZ) This dosn't really mean much to me....I think its a problem with my struct. Here is the code relating to it:
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL_TTF.lib")

#include <stack>
#include "SDL.h"
#include "SDL_TTF.h"

#include "defines.h"
#include "Image.h"
#include "Timer.h"
#include "Input.h"

struct states
{
	void (*statePointer)();
};

//globals
std::stack<states> g_stateStack;

Image* g_screen = NULL;
Timer* g_timer = NULL;
Input* g_input = NULL;

SDL_Surface* g_bitmap = NULL;

//prototypes
void menu();
void game();
void exit();

void init();
void shutDown();

void handleInput();

int main(int argc, char **argv)
{
	init();
	
	//game loop
	while(!g_stateStack.empty())
	{
		g_stateStack.top().statePointer();
	}

	shutDown();

	return 0;
}

void init()
{
	g_screen = new Image(WINDOW_WIDTH, WINDOW_HEIGHT,
						 WINDOW_CAPTION, 
						 0 ,0 ,0);
	g_timer = new Timer();

	g_input = new Input();

	g_timer->timeSinceCreation();

	g_screen->loadBitmap("data/background.bmp", 255, 0, 255);
	
	//load up our function pointers.
	states exitFunc;
	exitFunc.statePointer = exit;
	g_stateStack.push(exitFunc);

	states menuFunc;
	menuFunc.statePointer = menu;
	g_stateStack.push(menuFunc);
}

void shutDown()
{
	g_screen->closeBitmap(g_bitmap);

	delete g_screen;
	delete g_timer;
	delete g_input;
}

void menu()
{
	if((g_timer->timeSinceLastFrame() - g_timer->timeSinceCreation()) >= FRAME_RATE)
	{
		handleInput();
		
		g_screen->beginScene();

		g_screen->drawText("Start (G)ame", 12, 350, 250, 255, 255, 255, 0, 0, 0);
		g_screen->drawText("(Q)uit game", 12, 350, 270, 255, 255, 255, 0, 0, 0);

		g_screen->endScene();

		g_timer->timeSinceLastFrame();
	}
}

void game()
{
	if((g_timer->timeSinceLastFrame() - g_timer->timeSinceCreation()) >= FRAME_RATE)
	{
		handleInput();
		
		g_screen->beginScene();

		g_screen->drawText("Game", 12, 350, 250, 255, 255, 255, 0, 0, 0);

		g_screen->endScene();

		g_timer->timeSinceLastFrame();
	}	
}

void exit()
{
	if((g_timer->timeSinceLastFrame() - g_timer->timeSinceCreation()) >= FRAME_RATE)
	{
		handleInput();
		
		g_screen->beginScene();

		g_screen->drawText("exit Game (Y or N)?", 12, 350, 250, 255, 255, 255, 0, 0, 0);

		g_screen->endScene();

		g_timer->timeSinceLastFrame();
	}	
}

void handleInput()
{
	//get all the keys from the keyboard
	bool* keysHeld = g_input->getInput();

	//general input for all functions
	if(g_input->windowClosed())
	{
		while(!g_stateStack.empty())
			g_stateStack.pop();
	}

	if(keysHeld[SDLK_ESCAPE])
	{
		g_stateStack.pop();
	}
	if(keysHeld[SDLK_q])
	{
		g_stateStack.pop();
	}

	//make sure we are on the right screen before allowing us to do this,
	//otherwise we might push multipule game()


	//g will start the game
	if(g_stateStack.top().statePointer == menu)
	{
		if(keysHeld[SDLK_g])
		{
			states temp;
			temp.statePointer = game;
			g_stateStack.push(temp);
			return;
		}
	}

	//do you want to exit?
	if(g_stateStack.top().statePointer == exit)
	{
		if(keysHeld[SDLK_y]) //yes
		{
			g_stateStack.pop();
			return; //game over, leave the function
		}
		if(keysHeld[SDLK_n])
		{
			states temp;
			temp.statePointer = menu;
			g_stateStack.push(temp);
			return; //leave the function to get to the menu
		}
	}
}
I'm pretty sure I got the syntax right so I'm not sure why I have this problem, if you want my other files I will put them up. Also sorry if i posted too much code but I really don't know where this error is. Thanks in advance for any help and appologies if this is a realy obvious problem.

Share this post


Link to post
Share on other sites
This is a linker error, not a syntax error.

I'd say you need to make sure that you're using the correct debug runtime library in your project settings.

What command line options are being passed to the linker?

What version of Visual Studio are you using?

Share this post


Link to post
Share on other sites
Hi, thanks for the reply.

I'm using VS 2008 Express and Vista home premium, the only thing I changed in regards to the linker was the runtime library from Multi-threaded Debug DLL (/MDd) to Multi-threaded DLL (/MD). Is there something else I should have changed?

Share this post


Link to post
Share on other sites
Problem solved!

I had a friend help me out with this one, he said that there are problems when using STL and SDL with VS 2008 and taught me how to ban certain debug librarys that can cause this sort of problem. I also changed the DLL back to the debug one and that allowed it to compile, strange, I don't think I understand the linker DLL system yet :p.

This topic can be closed now, thanks guys!

Now to get back to coding....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this