Function Pointers and AI

Started by
5 comments, last by Flying_Dagger 17 years, 8 months ago
Im currently learning how to implement function pointers for my 2D game project. Im planning to use them to run AI for the game. The idea is to have a single class called TaskManager which has the collection of all the AI functions and a Object struct thats stores and run an AI function on under certain condition. The codes i have written so far for testing: AppMain.cpp

include <iostream>
#include "Board.h"
#include "TaskMgr.h"
using namespace std;

void main (void) {
	Board	* newBoard;


	newBoard = new Board();

	newBoard->test->mf_RunAI();

	cout<<newBoard->test->haha;
}


Board.h and .cpp where the game is runned

class TaskMgr;

struct Unit {
	int		haha;
	TaskMgr		* Mgr;
	void		(TaskMgr::*pt2member)(Unit *);
	void		mf_RunAI() { (Mgr->*pt2member)(this); };
};

class Board
{
public:
	Board(void);
	~Board(void);

	Unit			* test;
	TaskMgr			* mTMgr;
};



Board::Board(void)
{
        mTMgr = new TaskMgr();
	test = new Unit;
	mTMgr->SetUp(test);
}

Board::~Board(void)
{
}


TaskMgr.h and .cpp where the tasks are assigned to units

struct Unit;

class TaskMgr
{
public:
	TaskMgr(void);
	~TaskMgr(void);

	void SetUp(Unit *);
	void Addition(Unit *);
	void Subtraction(Unit *);
};



TaskMgr::TaskMgr(void)
{
}

TaskMgr::~TaskMgr(void)
{
}

void TaskMgr::SetUp(Unit * target) {
	target->haha = 0;
	target->Mgr = this;
	target->pt2member = &TaskMgr::Addition;
}

void TaskMgr::Addition(Unit * target) {
	target->haha++;
}

void TaskMgr::Subtraction(Unit * target) {
	target->haha--;
}


The code compiles fine but when i run it there is an access violation error that occurs at the line. void mf_RunAI() { (Mgr->*pt2member)(this); }; Please help me out with this as it is very important to the game. Thanks. P.S. Im sorry that i did not put the codes in a code tag because i don't know how to. If anyone can tell me how please do. Edit: Codes in source tag thanks to silothesuper :P [Edited by - Flying_Dagger on August 20, 2006 3:05:30 AM]
Advertisement
Put [ source ] before your code and [ /source ] after it. Do them without the spaces.
It seems me that you call SetUp of the Board's test Unit by using an uninitialized pointer to a TaskMgr:

newBoard = new Board();

invokes

test = new Unit;
mTMgr->SetUp(test);

where mTMgr isn't initialized yet. Hence SetUp doesn't really set-up the test member, and the later occuring

newBoard->test->mf_RunAI();

doesn't find a valid pt2member then. But maybe I'm wrong here.


P.S.: You should write your c'tors to initialize the members. Under the assumption that my above analysis is correct, then e.g. if you would have written

Board::Board()
: mTMgr(static_cast<TaskMgr*>(0)) {}

then the access to mTMgr would have failed already earlier, and the error's cause were not hidden.
Oh right... that was a mistake and i changed it. It still doesn't work thought, the same error occurs at the same place.
Hmm. There are some flaws like that main() should return an int, but that hasn't anything to do with the problem. I've copied your source snippets into one file, compiled it with g++ 3.3.5 under linux, and it works fine for both TaskMgr::Addition and TaskMgr::Subtraction. Are you ignoring any warnings the compiler or linker is spitting out?
lol it worked huh..

i cleaned and rebuilded with VS .Net 2003. There is no warning whatsoever.

But then again.. having it working in a different compiler is still hope :) Maybe there's something that has been overlooked, something simple. hmmmmmmm
Woot! Ok... i found out the error. Apparently I did not include the TaskMgr.h in my Board.h but instead in Board.cpp and proceed with delcaring a TaskMgr class without any body. As such when the Unit calls the Addition function in the TaskMgr it refers to the TaskMgr class without anybody so results in the error.

Thanks a lot for the help. :P

This topic is closed to new replies.

Advertisement