# Forward declaration and a singleton class

This topic is 2450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having an issue with my Manager class is a singleton and at the bottom of my header and all the classes I can add to it are above it. Problem is I would like to access the Manager class from some of the classes above it in the header and I can't figure out how to forward declare it?

 class GetInput : public Event { public: explicit GetInput(const std::string& id) : Event(id){} virtual ~GetInput(){} virtual void Run(void) { std::cout << "Enter input:"; sf::sleep(sf::seconds(10.0f)); std::string s; std::cin >> s; if(s == "quit") NX::EventManager::Get()->SetLoop(false); } }; class Manager// defined here... 

##### Share on other sites
Just move the function definitions that use the singleton from the class body to after the singleton is defined.

##### Share on other sites
 class Manager; // forward declaring here... class GetInput : public Event { public: explicit GetInput(const std::string& id) : Event(id){} virtual ~GetInput(){} virtual void Run(void) { std::cout << "Enter input:"; sf::sleep(sf::seconds(10.0f)); std::string s; std::cin >> s; if(s == "quit") NX::EventManager::Get()->SetLoop(false); } }; class Manager// defined here... { } 

Note the first line, you need to forward declaring there.

##### Share on other sites

Note the first line, you need to forward declaring there.

That's useless. A forward declaration simply says "a class of that name exists somewhere". It will NOT actually let you use that class, because the class is still undefined and it is completely unknown what is IN that class.

A forward declaration let's you do pretty much nothing but pass around pointers/references to that class. You still can't declare a variable of that type (because the size is unknown) and you can't call any functions of it (because they don't exist without actually DEFINING the class and what it looks like).

The simple matter of fact is that you can't "use" a forward declared class.

Fixing dependencies like that is usually based on NOT dumping your implementation in the header file, though in this case as SiCrane already said: DEFINE the class, before you try to use it.

##### Share on other sites

[quote name='wqking' timestamp='1333243884' post='4927069']
Note the first line, you need to forward declaring there.

That's useless. A forward declaration simply says "a class of that name exists somewhere". It will NOT actually let you use that class, because the class is still undefined and it is completely unknown what is IN that class.

A forward declaration let's you do pretty much nothing but pass around pointers/references to that class. You still can't declare a variable of that type (because the size is unknown) and you can't call any functions of it (because they don't exist without actually DEFINING the class and what it looks like).

The simple matter of fact is that you can't "use" a forward declared class.

Fixing dependencies like that is usually based on NOT dumping your implementation in the header file, though in this case as SiCrane already said: DEFINE the class, before you try to use it.
[/quote]
What you said is that I replied in another forward declaring thread days ago. :-)
Yes, the OP need to move the function body to source file, or after the Manager class in the header, if NX::EventManager is the singleton manager (is it?).
My that kind of forward declaring only allows the "above" classes declare fields or function parameters as pointer/reference to Manager, but can't use the pointers/references until the Manager is defined.

##### Share on other sites
Thanks guys, what Trienco said is what I tried and that solved it. I guess I get so automatically inlined though bound due to dumping everything into the header I forgot about this....

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5
A4L
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013602
×