Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

AI Finite state error error C2143:

Started by boolai Jun 28, 2010 at 11:42 PM 2 replies 1.8k views
Original Post
boolai
boolai
Been learning finite state machines with :Programming game AI by example book". So for the past week I have been doing very well. Except for today I can not seem to compile any of the code I did. I keep getting this error error C2143: syntax error : missing ';' before '.'.

#ifndef _ENTITY_H_#define _ENTITY_H_#pragma once#include "allegro.h"#include "Sprite.h"class State;class entity{public:	entity(void);	~entity(void);	State* m_pCurrentState;	State* m_pPreviousState;	Sprite m_sprite;	void Update();	void ChangeState( State* p_newState );};#endif #include "entity.h"#include "state.h"entity::entity(void){	m_pCurrentState = NULL;	m_pPreviousState = NULL;}entity::~entity(void){}void entity::Update(){	m_pCurrentState->execute(this);}void entity::ChangeState( State* p_newState) {	if( m_pCurrentState != NULL )	{			//Save the previous state		m_pPreviousState = m_pCurrentState;		//Delete the current state		delete m_pCurrentState;	}//end if	else{		m_pPreviousState = p_newState;	}	//change the state	m_pCurrentState = p_newState;}</code> here is the states<code>#include "entity.h"#ifndef _STATE_H_#define _STATE_H_#pragma onceclass entity;class State_Right;class State_Left;class State_Down;class State_UP;//The state abstract classclass State{public:	int prev;	virtual void execute( entity* p_e )=0;};//State right change the direction of the entity to go rightclass State_Right : public State{private:	public:	State_Right();	void execute( entity* p_e );};//State go left////////////////////////////////////////class State_Left : public State{private:	public:	State_Left();	void execute( entity* p_e );};//State go down///////////////////////////////////////class State_Down_Left : public State{private:	public:	State_Down_Left();	void execute( entity* p_e );};//State go down///////////////////////////////////////class State_Down_Right : public State{private:	public:	State_Down_Right();	void execute( entity* p_e );};//State go up////////////////////////////////////////class State_Up : public State{private:	public:	State_Up();	void execute( entity* p_e );};#include "State.h"void State::execute( entity *p_e ){}////////////////////State Right//////////////State_Right::State_Right(){	prev =0;}void State_Right::execute(entity *p_e){	static int delay = 10;	static int timer =0;	static int space = 0;	if( timer >= delay )	{		timer = 0;		space = p_e->m_sprite.getSpriteStats()->width;	}//end if	else{		++timer;		space=0;	}//end else	//Move the sprite	p_e->m_sprite.setPos(p_e->m_sprite.getXpos()+space, p_e->m_sprite.getYpos());	if( p_e->m_sprite.getXpos()+p_e->m_sprite.getSpriteStats()->width >= SCREEN_W )	{		p_e->ChangeState( new State_Down_Left());	}}//State_Down//////////////////////////////State_Down_Left::State_Down_Left(){	prev =3;}void State_Down_Left::execute(entity *p_e){	//Move the sprite down on then switch to the left	p_e->m_sprite.setPos(p_e->m_sprite.getXpos(), p_e->m_sprite.getYpos()+p_e->m_sprite.getSpriteStats()->height);	p_e->ChangeState(new State_Left());	if( p_e->m_sprite.getYpos() >= SCREEN_H )	{		p_e->m_sprite.setPos(0,0);	}}//////////////////////////////////////////State_Down_Right::State_Down_Right(){	prev =3;}void State_Down_Right::execute(entity *p_e){	//Move the sprite down on then switch to the left	p_e->m_sprite.setPos(p_e->m_sprite.getXpos(), p_e->m_sprite.getYpos()+p_e->m_sprite.getSpriteStats()->height);	p_e->ChangeState(new State_Right());	if( p_e->m_sprite.getYpos() >= SCREEN_H )	{		p_e->m_sprite.setPos(0,0);	}}//State_Left////////////////////////////State_Left::State_Left(){	prev =1;}void State_Left::execute(entity *p_e){	static int delay = 10;	static int timer =0;	static int space = 0;	if( timer >= delay )	{		timer = 0;		space = p_e->m_sprite.getSpriteStats()->width;	}//end if	else{		++timer;		space=0;	}//end else	//Move the sprite	p_e->m_sprite.setPos(p_e->m_sprite.getXpos()-space, p_e->m_sprite.getYpos());	if( p_e->m_sprite.getXpos() <= 0 )	{		p_e->ChangeState( new State_Down_Right());	}}//State_Up////////////////////////////////State_Up::State_Up(){}#endif </code>Here is the main<code> #include "allegro.h"#include "entity.h"entity e;e.ChangeState(new State_Right);//main functionvoid main(void){    //main loop    return;}END_OF_MAIN()Thanks in advance for your help[IFox: Added source tags]


[Edited by - InnocuousFox on June 29, 2010 5:20:31 PM]
Just trying this learn this very challenging subject of game programming.
alexjc
alexjc
What line does the error occur? You know you can format-up the code using a markup element.

[SOURCE]ABC[/SOURCE]
Join us in Vienna for the nucl.ai Conference 2015, on July 20-22... Don't miss it!
earl
earl
entity e;e.ChangeState(new State_Right);//main functionvoid main(void){...


The error message is not very helpful, but basically you should just avoid calling functions in global scope, with a possible exception of initializing globals (which are somewhat frowned upon too). I'm not absolutely sure why this doesn't compile, or even whether it should, but the MSVC compiler seems to assume all global code is either a definition or variable (possibly being assigned to by a function call).

Shifting the ChangeState member function call (and the entity definition too, for that matter) inside the main function should fix your problem.
alvaro
alvaro
You can't just put code outside a function. Besides that, void main(void)' is not a valid prototype for main', and you can't name a macro _ENTITY_H_ because identifiers that start with underscore followed by a capital letter are reserved to the implementation.

Quote:
17.4.3.2.1 Global names [lib.global.names]
Each name that contains a double underscore ("__") or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.