Sign in to follow this  
MARS_999

Forward declaration and a singleton class

Recommended Posts

MARS_999    1627
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?

[code]
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...
[/code]

Share this post


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

Share this post


Link to post
Share on other sites
wqking    761
[CODE]
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...
{

}
[/CODE]

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

Share this post


Link to post
Share on other sites
Trienco    2555
[quote name='wqking' timestamp='1333243884' post='4927069']
Note the first line, you need to forward declaring there.
[/quote]

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 this post


Link to post
Share on other sites
wqking    761
[quote name='Trienco' timestamp='1333266887' post='4927117']
[quote name='wqking' timestamp='1333243884' post='4927069']
Note the first line, you need to forward declaring there.
[/quote]

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 this post


Link to post
Share on other sites
MARS_999    1627
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.... :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this