Error building class "no appropriate default constructor available"

It appears that since the StateManager class includes (Engine& engine), it might be getting involved with the actual 'Class Engine' functions.

I've been cycling through a few different single errors, I change something and a get an error, I change something else and I get a different error etc.

This is probably the source so can yall help me out?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Engine;
class StateManager
{
public:
    StateManager(Engine& engine);
};

class Engine
{
public:
    Engine();
    StateManager Sta_Mgr;
};

Engine::Engine(){}
Last edited on
StateManager has no default constructor, Engine includes an instance of StateManager, and Engine's constructor doesn't explicitly call StateManager's constructor.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
Alright, I updated it a little bit, and I went through a couple of errors and now I am at:

"error C2758: 'StateManager::theEngine' : must be initialized in constructor base/member initializer list"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Engine;
class StateManager
{
public:
    StateManager();
    StateManager(Engine& engine);
private:
    Engine& theEngine;
};

StateManager::StateManager(){}

StateManager::StateManager(Engine& engine): 
theEngine(engine){}


class Engine
{
public:
    Engine();
    StateManager Sta_Mgr;
};
Engine::Engine(){}

int main()
{
	Engine Game;
}


I guess I need to initialize "theEngine", but I'm kind of confused.

Why do I need to initialize it? StateManager::StateManager(), it thought it is just meant to be an empty default constructor.
How exactly would I initialize it?
I thought it is supposed to be initialized when I start the program.
Last edited on
The reference must be initialized with the object that holds it. Or you could use a pointer.
Oh, yea I changed up the code after I posted the error here, lol.

@filipe, could you possibly give me an example?
Topic archived. No new replies allowed.