really strange error I haven't seen before

Hey guys,

So I'm working on making a finite state machine for an AI class at uni, and I have the weirdest of errors right now, been working on it and nothing seems to get rid of them so hoped someone here can help :)

Basically, in my entity class (the thing that uses the FSM), it doesn't seem to be recognising the State class type, the only thing I could think is the problem now is the order in which I include things but cant seem to get the order right, I know its most likely the smallest of problems, always is so some fresh eyes will be really useful :)

Entity class:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef ENTITY_H
#define ENTITY_H

using namespace std;

#include "State.h"

class Entity
{
private:
	//static int nextID;

	//int myID;

	State* state;

	/*void setID(int id)
	{
		if(id >= nextID)
		{
			myID = id;
			nextID++;
		}
	}*/

public:
	Entity(int id)
	{
		//setID(id);
	}

	Entity(int id, State* s)
	{
		//setID(id);
		state = s;
		state->enter(this);
	}

	virtual void update();

	int getID()
	{
		//return myID;
	}

	virtual ~Entity()
	{

	}
};

#endif 


main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

//using namespace std;

#include "LinkedList.h"
#include "Graph.h"
#include "State.h"
#include "Entity.h"

int main(int argc, char** argv)
{
	Graph<State> stateMachine();

	return 0;
};


The things to do with ID in entity are commented out as they're a work in progress, as is the rest of my code so inefficient stuff will be sorted out when I can test it again heh

If you need anything else, just say and Ill get right back to you :)
where is the error message?
Probably something I should have pointed out sooner I guess :P

and its on any line that uses State or refers to an instance of State after State* state;

they're all things like 'must me a pointer to class type', Entity(int id, State* s) has an error that says Entity(int) already defined, as though the state part isn't in there

the errors I get I know what they mean but not why they are there
So, if anything that says "State" causes an error, why have you given us everything BUT the source code that defines State? We need to see that too, you know ;)
Last edited on
*facepalms* sorry guys, its been a long day haha, not enough coffee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef STATE_H
#define STATE_H

#include "Entity.h"

class State
{
protected:
	State(){}

public:
	virtual void enter(Entity* e) = 0;

	virtual void execute(Entity* e) = 0;

	virtual void exit(Entity* e) = 0;

	virtual ~State(){}
};

#endif 


dont believe I see any errors here but I may be wrong

heres the tired state I made, just an example of one of the subclasses:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef TIREDSTATE_H
#define TIREDSTATE_H

#include "State.h"
#include "Entity.h"

class TiredState : public State
{
private:
	static TiredState* instance = nullptr;
	TiredState() : State()
	{

	}

public:
	TiredState* get()
	{
		if(instance == nullptr)
		{
			instance = new TiredState();
		}
		return instance;
	}

	void enter(Entity* e)
	{
		//entity = e;
	}

	void execute(Entity* e)
	{

	}

	void exit(Entity* e)
	{

	}

	~TiredState()
	{
		delete instance;
	}
};

#endif 
Aha, you have a circular dependency: State and Entity both use each other, but one definition has to come first.

Read: http://www.cplusplus.com/forum/articles/10627/
it explains how to fix the problem with forward declaring.
ah ok thanks :)

so am I right in thinking I should forward declare entity in state with that only being a pointer?

and that is just 'class Entity;'?

never actually done this, maybe its something my tutors should have pointed out...
Yes, in this case a forward declaration of Entity in State.h will work fine.
Last edited on
well, it works now :), decided I should just forward declare entity in state as that doesn't do a massive amount with state I don't think

would it be better to forward declare state in entity too or leave that as an include? still a little confused heh, is it only used when there's just a pointer as a parameter or can it be used when you're working with the pointer too?
You've solved the circular dependency problem, you do not need to do anything else to solve the solution. ;)
yeah I know, was just wondering whether the forward declaration is only used when pointers are used as just parameters and return values or if you use them for when you're doing things with pointers or both :)

you've been a big help though so many thanks :D
Forward declaring is used for when you need to resolve a circular dependency at some point, and it works when you use pointers because the compiler doesn't need information about how the class is defined. You do not need to use forward declaring whenever you use pointers; it is used mainly for solving circular dependencies.

In other words, if you use a larger oven to bake a larger cake, you don't need to use larger ovens for all your cakes, just the large cakes.
haha, nice analogy, and thanks again :)
Topic archived. No new replies allowed.