4 Replies - 730 Views - Last Post: 06 April 2013 - 07:57 PM Rate Topic: -----

#1 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

need assistance on errors with pre-compiled code

Posted 06 April 2013 - 05:01 PM

Still researching Finite State Machines, I took this code from http://www.drdobbs.c...84401236?pgno=2

here are the files maybe I did something wrong, I got 40 errors that I'm unsure why one culprit I know is in the first file and the other in the #define Transition_Map, Please help, I need to understand this code so I can write my own for a FSM for an AI engine for homework this week. If I can understand the below and the errors I'm getting and why I'll be able to write my own. Thanks. Also if any of you have any ideas on how to write an FSM for an enemy player providing it states of alive, dead, attack, patrol, that would be great! I've been trying to write it using Mat Bucklands book but I'm falling short. I believe by the code below, (if you can help me get it to work) I can solve this problem. Thanks in advance.

EventData.h

#ifndef EVENT_DATA_H
#define EVENT_DATA_H

class EventData
{
public:
	virtual ~EventData() {};
};#endif //EVENT_DATA_H



StateMachine.h


#ifndef STATE_MACHINE_H
#define STATE_MACHINE_H
#include "EventData.h" 

struct StateStruct;
// base class for state machines
class StateMachine
{
public:
	StateMachine(int maxStates);
	virtual ~StateMachine() {}
protected:
	enum { EVENT_IGNORED = 0xFE, CANNOT_HAPPEN };
	unsigned char currentState;
	void ExternalEvent(unsigned char, EventData* = NULL);
	void InternalEvent(unsigned char, EventData* = NULL);
	virtual const StateStruct* GetStateMap() = 0;
private:
	const int _maxStates;
	bool _eventGenerated;
	EventData* _pEventData;
	void StateEngine(void);
};
typedef void (StateMachine::*StateFunc)(EventData *);
struct StateStruct 
{
	StateFunc pStateFunc;
};
#define BEGIN_STATE_MAP \
public:\
	const StateStruct* GetStateMap() {\    
static const StateStruct StateMap[] = {  
#define STATE_MAP_ENTRY(entry)\
	{ reinterpret_cast<StateFunc>(entry) }, 
#define END_STATE_MAP \   
	{ reinterpret_cast<StateFunc>(NULL) }\ 
}; \
return &StateMap[0]; }
#define BEGIN_TRANSITION_MAP \
	static const unsigned char TRANSITIONS[] = {
#define TRANSITION_MAP_ENTRY(entry)\
	entry, 
#define END_TRANSITION_MAP(data) \
	0 };\
	ExternalEvent(TRANSITIONS[currentState], data);
#endif //STATE_MACHINE_H 




StateMachine.cpp



#include "StateMachine.h"

StateMachine::StateMachine(int maxStates) :
_maxStates(maxStates),
	currentState(0),
	_eventGenerated(false),
	_pEventData(NULL)
{
}

// generates an external event. called once per external event
// to start the state machine executing
void StateMachine::ExternalEvent(unsigned char newState,
	EventData* pData)
{ 
	// if we are supposed to ignore this event  
	if (newState == EVENT_IGNORED) {
		// just delete the event data, if any   
		if (pData)  
			delete pData;
	}
	else {
		// generate the event and execute the state engine  
		InternalEvent(newState, pData);
		StateEngine();
	}
}

// generates an internal event. called from within a state
// function to transition to a new state
void StateMachine::InternalEvent(unsigned char newState,
	EventData* pData)
{
	_pEventData = pData;
	_eventGenerated = true;
	currentState = newState;
}

// the state engine executes the state machine states
void StateMachine::StateEngine(void)
{
	EventData* pDataTemp = NULL;
	// TBD - lock semaphore here
	// while events are being generated keep executing states 
	while (_eventGenerated) { 
		pDataTemp = _pEventData;  // copy of event data pointer
		_pEventData = NULL;       // event data used up, reset ptr 
		_eventGenerated = false;  // event used up, reset flag 
		
		assert(currentState < _maxStates); 
		
		// execute the state passing in event data, if any
		const StateStruct* pStateMap = GetStateMap();
		(this->*pStateMap[currentState].pStateFunc)(pDataTemp);
		// if event data was used, then delete it
		if (pDataTemp) { 
			delete pDataTemp;
			pDataTemp = NULL; 
		}
	}
	// TBD - unlock semaphore here
}




Motor.h


#ifndef MOTOR_H
#define MOTOR_H
#include "StateMachine.h"

// structure to hold event data passed into state machine
struct MotorData : public EventData
{
	int speed;
};
// the Motor state machine class
class Motor : public StateMachine
{
public: 
	Motor() : StateMachine(ST_MAX_STATES) {}
	// external events taken by this state machine
	void Halt();  
	void SetSpeed(MotorData*);
private:
	// state machine state functions
	void ST_Idle();
	void ST_Stop();
	void ST_Start(MotorData*);
	void ST_ChangeSpeed(MotorData*);
	
	// state map to define state function order
	BEGIN_STATE_MAP
		STATE_MAP_ENTRY(ST_Idle)
		STATE_MAP_ENTRY(ST_Stop)
		STATE_MAP_ENTRY(ST_Start)
		STATE_MAP_ENTRY(ST_ChangeSpeed)
	END_STATE_MAP  
	// state enumeration order must match the order of state 
	// method entries in the state map 
	enum E_States { 
		ST_IDLE = 0, 
		ST_STOP,
		ST_START,
		ST_CHANGE_SPEED, 
		ST_MAX_STATES 
	};
};
#endif //MOTOR_H




Motor.cpp



#include "Motor.h"

// halt motor external event
void Motor::Halt(void)
{ 
	// given the Halt event, transition to a new state based upon 
	// the current state of the state machine
	BEGIN_TRANSITION_MAP                      // - Current State -
		TRANSITION_MAP_ENTRY (EVENT_IGNORED)  // ST_Idle 
		TRANSITION_MAP_ENTRY (CANNOT_HAPPEN)  // ST_Stop
		TRANSITION_MAP_ENTRY (ST_STOP)        // ST_Start
		TRANSITION_MAP_ENTRY (ST_STOP)        // ST_ChangeSpeed
	END_TRANSITION_MAP(NULL)
}
// set motor speed external event
void Motor::SetSpeed(MotorData* pData)
{
	BEGIN_TRANSITION_MAP                      // - Current State -
		TRANSITION_MAP_ENTRY (ST_START)       // ST_Idle
		TRANSITION_MAP_ENTRY (CANNOT_HAPPEN)  // ST_Stop
		TRANSITION_MAP_ENTRY (ST_CHANGE_SPEED)// ST_Start 
		TRANSITION_MAP_ENTRY (ST_CHANGE_SPEED)// ST_ChangeSpeed
	END_TRANSITION_MAP(pData)
}

// state machine sits here when motor is not running
void Motor::ST_Idle() 
{
}

// stop the motor 
void Motor::ST_Stop()
{
	// perform the stop motor processing here
	// transition to ST_Idle via an internal event
	InternalEvent(ST_IDLE);
}

// start the motor going
void Motor::ST_Start(MotorData* pData)
{ 
	// set initial motor speed processing here
} 

// changes the motor speed once the motor is moving
void Motor::ST_ChangeSpeed(MotorData* pData)
{
	// perform the change motor speed to pData->speed here
}




Here are the errors I am receiving

Error 1 error C2014: preprocessor command must start as first nonwhite space c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 8 1 DrDobbsFSM

Error 2 error C1070: mismatched #if/#endif pair in file 'c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h' c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 9 1 DrDobbsFSM

Error 3 error C2014: preprocessor command must start as first nonwhite space c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 8 1 DrDobbsFSM

Error 4 error C1070: mismatched #if/#endif pair in file 'c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h' c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 9 1 DrDobbsFSM

5 IntelliSense: PCH warning: header stop not at file scope. An intellisense PCH file was not generated. c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.h 42 16 DrDobbsFSM

6 IntelliSense: identifier "NULL" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 15 49 DrDobbsFSM

7 IntelliSense: identifier "NULL" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 16 49 DrDobbsFSM

8 IntelliSense: identifier "StateStruct" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 17 16 DrDobbsFSM

9 IntelliSense: identifier "NULL" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 36 32 DrDobbsFSM

10 IntelliSense: unrecognized token c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 36 39 DrDobbsFSM

11 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 38 1 DrDobbsFSM

12 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 38 22 DrDobbsFSM

13 IntelliSense: expected an expression c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 9 44 DrDobbsFSM

14 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 10 3 DrDobbsFSM

15 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 10 41 DrDobbsFSM

16 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 11 3 DrDobbsFSM

17 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 11 41 DrDobbsFSM
18 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 12 3 DrDobbsFSM

19 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 12 41 DrDobbsFSM

20 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 13 3 DrDobbsFSM

21 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 13 41 DrDobbsFSM

22 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 14 2 DrDobbsFSM

23 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 15 1 DrDobbsFSM

24 IntelliSense: identifier "MotorData" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 17 22 DrDobbsFSM

25 IntelliSense: expected an expression c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 19 44 DrDobbsFSM

26 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 20 3 DrDobbsFSM

27 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 20 41 DrDobbsFSM
28 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 21 3 DrDobbsFSM

29 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 21 41 DrDobbsFSM

30 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 22 3 DrDobbsFSM

31 IntelliSense: expected an identifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 22 41 DrDobbsFSM

32 IntelliSense: this declaration has no storage class or type specifier c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 37 2 DrDobbsFSM

33 IntelliSense: identifier "ST_IDLE" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 37 16 DrDobbsFSM

34 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 38 1 DrDobbsFSM

35 IntelliSense: identifier "MotorData" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 41 22 DrDobbsFSM

36 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 44 1 DrDobbsFSM

37 IntelliSense: identifier "MotorData" is undefined c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 47 28 DrDobbsFSM

38 IntelliSense: expected a declaration c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\motor.cpp 50 1 DrDobbsFSM

39 IntelliSense: the #endif for this directive is missing c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 1 2 DrDobbsFSM

40 IntelliSense: '#' not expected here c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 8 3 DrDobbsFSM

Is This A Good Question/Topic? 0
  • +

Replies To: need assistance on errors with pre-compiled code

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: need assistance on errors with pre-compiled code

Posted 06 April 2013 - 05:30 PM

The very first error is fairly clear:

Quote

Error 1 error C2014: preprocessor command must start as first nonwhite space c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 8 1 DrDobbsFSM


If you look at line 8 in the file eventdata.h then the preprocessor command #endif must start at the beginning of a line.

You should take the time to read through the errors, starting with the first one and, if necessary, do some research to find out what it means.

You might have 40 errors but sometimes they might all disappear having made one correct.

Most of these errors are "mismatched this", "expected that". These are straight-forward syntax or typing errors and a decent editor or IDE will highlight or underline these, and probably produce helpful messages as well.

This post has been edited by andrewsw: 06 April 2013 - 05:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: need assistance on errors with pre-compiled code

Posted 06 April 2013 - 05:34 PM

View Postandrewsw, on 06 April 2013 - 06:30 PM, said:

The very first error is fairly clear:

Quote

Error 1 error C2014: preprocessor command must start as first nonwhite space c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\eventdata.h 8 1 DrDobbsFSM


If you look at line 8 in the file eventdata.h then the preprocessor command #endif must start at the beginning of a line.

You should take the time to read through the errors, starting with the first one and, if necessary, do some research to find out what it means.

You might have 40 errors but sometimes they might all disappear having made one correct.

Most of these errors are "mismatched this", "expected that". These are straight-forward syntax or typing errors and a decent editor or IDE will highlight or underline these, and probably produce helpful messages as well.



Thanks, damn I can't believe I didn't see that, now I got 51 errors hopefully I can figure this out
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,883
  • Joined: 25-December 09

Re: need assistance on errors with pre-compiled code

Posted 06 April 2013 - 06:38 PM

The number of errors also suggests that you waited too long to try to compile this program. You should be compiling often, sometimes after adding just one line of code. This will help to keep the number of errors to a manageable number and you will also know where to start looking for the errors.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: need assistance on errors with pre-compiled code

Posted 06 April 2013 - 07:57 PM

still can't locate what is causing this error

#ifndef STATE_MACHINE_H
#define STATE_MACHINE_H
#include <stdio.h>
#include "EventData.h" 

struct StateStruct;
// base class for state machines
class StateMachine
{
public:
	StateMachine(int maxStates);
	virtual ~StateMachine() {}
protected:
	enum { EVENT_IGNORED = 0xFE, CANNOT_HAPPEN };
	unsigned char currentState;
	void ExternalEvent(unsigned char, EventData* = NULL);
	void InternalEvent(unsigned char, EventData* = NULL);
	virtual const StateStruct* GetStateMap() = 0;
private:
	const int _maxStates;
	bool _eventGenerated;
	EventData* _pEventData;
	void StateEngine(void);
};
typedef void (StateMachine::*StateFunc)(EventData *);
struct StateStruct 
{
	StateFunc pStateFunc;
};

#define BEGIN_STATE_MAP \
public:\
	const StateStruct* GetStateMap() {\    
static const StateStruct StateMap[] = {  

#define STATE_MAP_ENTRY(entry)\
	{ reinterpret_cast<StateFunc>(entry) }, 

#define END_STATE_MAP \   
	{ reinterpret_cast<StateFunc>(NULL) }\ 
}; \
return &StateMap[0]; }
#define BEGIN_TRANSITION_MAP \
	static const unsigned char TRANSITIONS[] = {
#define TRANSITION_MAP_ENTRY(entry)\
	entry, 
#define END_TRANSITION_MAP(data) \
	0 };\
	ExternalEvent(TRANSITIONS[currentState], data);
#endif //STATE_MACHINE_H 




error:

Error 1 error C2017: illegal escape sequence c:\users\gmoney93079\documents\visual studio 2010\projects\drdobbsfsm\drdobbsfsm\statemachine.h 40 1 DrDobbsFSM
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1