State machine prog

Hi,
I recently started to write a state machine, following the guidances from the book "programming game AI by example", from Mat Buckland.
A few months ago I made a graphic application, with shapes and different graphic elements reacting to a midi keyboard. I did that on VVVV (a graphic prog environment), which did not allow me much flexibility. So now i try with C++ and Opengl...

So that was for the background story... I decided to implement a state machine: each graphic element will be controlled thanks to the different states implemented. And, as I dont know much about AI and state machines, i started by implemented what Mat Buckland shows in the chapter 2 of his book, on finite state machines.

Right now there is neither opengl nor midi in the code, just some class declarations:

BaseEntity.h :
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
#ifndef BASE_ENTITY
#define BASE_ENTITY

#include <string>
#include<iostream>


class BaseEntity
{

    private:
    //Unique ID number:
    int m_ID;

    //Next valid ID number, updated for every instanciation:
    static int m_iNextValidID;

    //This is called within the constructor to make sure the ID is set correctlty.
    //It verifies that the value passed to the method is greater or equal to the
    //next valid ID:
    void SetID(int val);

    public:

    BaseEntity(int id);
    virtual ~BaseEntity();
    //Every shape must have an update function:
    virtual void Update()=0;
    int ID ()const;

};


#endif 


BaseEntity.cpp :
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
#include <string>
#include<iostream>
#include "BaseEntity.h"


    void BaseEntity::SetID(int val)
    {
        //Code here??
    }


    BaseEntity::BaseEntity(int id)
    {
        SetID(id);
    }


    BaseEntity::~BaseEntity(){}


    int BaseEntity::ID ()const
    {
        return m_ID;
    }


Shape.h :
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
#ifndef _SHAPE
#define _SHAPE

#include <string>
#include<iostream>

#include "BaseEntity.h"
#include "State.cpp"

class Shape : public BaseEntity
{
    private:
    //Pointer to a State instance:
    State* m_pCurrentState;

    //Shape position:
    float m_position [3];

    //ADD MORE ATTRIBUTES LATER (vertices...)

    public:
    Shape(int ID);

    void Update();

    //Change current state to new state:
    void ChangeState(State* pNewState);

    /*WRITE BULK OF INTERFACE LATER*/


};

#endif 


State.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include<iostream>
#include "Shape.h"

class State
{
    public:

    virtual ~State(){}

    //Executed when the state is entered:
    virtual void Enter(Shape*)=0;

    //Called when a shape is updated:
    virtual void Execute(Shape*)=0;

    //Executed when the state is exited:
    virtual void Exit(Shape*)=0;
};




As you will note, it's not yet really organized: State.cpp has the State class declaration (no State.h yet), and also, i didnt implement yet the Shape class, i only declared it. I just want to do things little by little, following the book!

The BaseEntity class is a base class from which will be derived all the future elements of the graphic app. The shape class is derived from it.

The State class is a virtual class, from which will later be derived specific states.

Each Shape instance will have a pointer to a State. The State instances will, through their Execute function, modify the Shape instances' arguments.

When I try to compile, I get 5 errors, all from State.cpp : three of them point to the 3 method declarations and say "'Shape' Has not been declared".

Then, I get "Redifinition of 'class State'", and "Previous definition of 'class State'".

To me, it seems that it's because the class State needs the class Shape declaration, and vice-versa. But the thing is that I can't find any way to avoid this problem!! And the author of the book i follow does not seem to bother about that...

Can anyone help me with that?? Thanks in advance!!
Hey, thanks dude!
The trick was to forward declare the State class in Shape.h...
Now i am trying to call the Execute function of the State class, from the State pointer in the Shape class (headache...), so I have to deal with inline functions...
I think I will go buy some aspirin...

But thanks a lot for the link, everything is there!
Topic archived. No new replies allowed.