In my program, there is an _Engine object, and I am using the Singleton design pattern to create a single object of it. In terms of my problem, there are 3 main files that should be known,
Engine.h - contains the class definition for Engine as follows:
class _Engine
{
public:
void UpdateTime();
void UpdateEnemies();
Image *GetEnemyImage() const;
static _Engine* Instance();
private:
float oldTime, deltaMilliSeconds;
vector<Enemy*> Enemy_Vector;
vector<Enemy*>::const_iterator iter;
Image *_enemyImage;
static _Engine *pinstance;
protected:
_Engine();
_Engine(const _Engine&);
_Engine& operator = (const _Engine&);
~_Engine();
};
Simple enough, most of it isn't related to the problem, but source always helps. And so next I have Engine.cpp which contains the definition for _Engine's functions and is as follows:
//---------- CONSTRUCTOR -----------//
_Engine::_Engine() : oldTime(ticks_to_millisecs(gettime())), Enemy_Vector(vector<Enemy*>()), _enemyImage(new wsp::Image)
{
srand(time(0));
_enemyImage->LoadImage("enemy.png");
deltaMilliSeconds = 0;
}
//---------- DESTRUCTOR -----------//
_Engine::~_Engine()
{
Enemy_Vector.clear();
}
//---------- UPDATE TIME -----------//
void _Engine::UpdateTime()
{
float newTime = ticks_to_millisecs(gettime());
deltaMilliSeconds = newTime - oldTime;
oldTime = newTime;
}
//---------- DRAW ENEMIEs -----------//
void _Engine::UpdateEnemies()
{
if (Enemy_Vector.empty())
{
for (iter = Enemy_Vector.begin(); iter != Enemy_Vector.end(); ++ iter)
{
(*iter)->Update(deltaMilliSeconds);
}
}
}
//---------- GET ENEMY IMAGE -----------//
inline wsp::Image *_Engine::GetEnemyImage() const
{
return _enemyImage;
}
//---------- Initialize Pointer ----------//
_Engine *_Engine::pinstance = 0;
//---------- INSTANCE -----------//
_Engine *_Engine::Instance()
{
if (pinstance == 0)
{
pinstance = new _Engine;
}
return pinstance;
}
The function most interesting above is *_engine::Instance(), basically it will cehck if pinstance has no address, if it has no address, it will create a new _Engine object and return it.
The other file file is Enemy.cpp, I won't post the source as their is little point at this stage. But the main problem is that I need Enemy.cpp to be able to see
_Engine *Engine = _Engine::Instance();
the Engine part so I can call it's functions, I tried making it extern in both Engine.h and Engine.cpp and including bother respectively in Enemy.cpp, but I just can't get it to work. If someone can help, I would greatly appreciate it.