0

I have two classes, both of which need to be able to call the same instance of entitymanager

class Engine
{
  EntityManager::Entitymanager EManager;
}

And I need to add an object to a vector contained by this particular instance of Engine. What I want to do is be able to add a bullet spawned by the player to the vector that contains all my entities.

class Player : Entity
{
  void SpawnBullet() {Engine::EManager.Add(BULLET);}
}

The above returns this error:

error: object missing in reference to ‘Engine::EManager’

How do I resolve this? Any help or pointers in the right direction would be much appreciated!

Dk43
  • 3
  • 2
  • 1
    The above does not return this error because the keyword is `class`, not `Class`. Please copy and paste your code; it's hard to answer the question when details in the question are wrong: who knows what other details are wrong? – James McNellis Jan 13 '11 at 05:46
  • Apologies, the typo on my behalf was a mistake copy pasted twice, it does not appear in the actual code. I've doublechecked to ensure there aren't any other mistakes. – Dk43 Jan 13 '11 at 05:49

2 Answers2

4

You are trying to access EManager without a class instance associated with it.

There are 2 solutions for this.

You have to have an instance of Engine around in order to access EManager:

class Engine {
    EntityManager::Entitymanager EManager;
};

Then you can access EManager this way:

m_engine.EManager.Add(BULLET)

You have to make EManager a static member of Engine (that is, it will be bound to the class scope only, you won't need an instance for it):

class Engine {
public:
    static EntityManager::Entitymanager EManager;
};

Then you can access it as you already did (It has to be public, or your classes have to be friends).

I feel however that you need to get a good introductory C++ book and understand what you are trying to achieve. And while you're at it, get one on software engineering too ;).

Community
  • 1
  • 1
Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
  • Cheers, this got everything working. I suspect you're right, I've been muddling along so far and I could do with working through a solid book on C++. – Dk43 Jan 13 '11 at 06:36
1

I have two classes, both of which need to be able to call the same instance of entitymanager

It seems you need to implement Singleton pattern. Have a look at the link, maybe you'll have a good design of your classes!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • After getting it to work with the above answer, I went off to read up on singletons as suggested, and then implemented one, and managed to tidy up my code quite a bit as a consequence. Cheers! – Dk43 Jan 13 '11 at 06:36