Original Post
I just got "Programming game AI by example" by Mat Buckland. His source is here I use his source to learn the singletion design pattern in the folder, buckland_AISource\Buckland_Chapter2-State Machines\WestWorld1 or WestWorldWithWoman The problem is that I write a source from scratch and end up to get an error, C2243. I couldn't find out how did I make the mistake. Here my sources: miner.h Miner.cpp state.h
#ifndef MINER_H
#define MINER_H
//#pragma once
#include "BaseEntity.h"
#include "State.h"
#include "Define.h"
#include "Miner_States.h"
class CState;
class CMiner : public CBaseEntity
{
public:
CMiner(int id);
~CMiner(void);
void Update();
void ChangeState(CState<CMiner>* pNewState);
void ChangeLocation(Location_Type location);
Location_Type Location(void);
bool PocketsFull(void);
void IncreaseGold(void){m_iGoldCarried++;};
int InBankValue(void){return m_iMoneyInBank;};
private:
int m_iGoldCarried;
int m_iMoneyInBank;
int m_iThirst;
int m_iFatigue;
Location_Type m_eLocation;
CState<CMiner>* m_pCurrentState;
};
#endif MINER_H
#include "Miner.h"
#include "State.h"
#include "Miner_States.h"
#include <assert.h>
//class CMiner;
class CState;
CMiner::CMiner(int id):CBaseEntity(id)
{
m_eLocation = BANK;
m_pCurrentState = CEnterMinerAndDig::Instance();
}
CMiner::~CMiner(void)
{
}
void CMiner::Update()
{
m_iThirst++;
if (m_pCurrentState)
m_pCurrentState->Execute(this);
}
void CMiner::ChangeState(CState<CMiner>* pNewState)
{
// make sure both states are valid
assert(m_pCurrentState && pNewState);
// call the exit method of the existing state
m_pCurrentState->Exit(this);
// change state to the new state
m_pCurrentState = pNewState;
// call the entry method of the new state
m_pCurrentState->Enter(this);
}
void CMiner::ChangeLocation(Location_Type location)
{
m_eLocation = location;
}
Location_Type CMiner::Location(void)
{
return m_eLocation;
}
bool CMiner::PocketsFull(void)
{
if (m_iGoldCarried > 5)
return true;
else
return false;
}
#ifndef STATE_H
#define STATE_H
template <class entity_type>
class CState
{
public:
virtual ~CState(void){};
// this will execute when the state is entered
virtual void Enter(entity_type*) = 0;
// this is called by the miner's update function each
update step
virtual void Execute(entity_type*) = 0;
// this will execute when the state is exited
virtual void Exit(entity_type*) = 0;
};
#endif STATE_H