I need some help to bebug some C++ code. I can't see any obvious errors but my compiler reported 36 errors. I think they are all relating to the first error I'm getting from inside the State class. It says: "changes meaning of 'State' to 'class State' but I'm not sure what that's telling me or what to do to fix it? THNX to anyone who responds w/help!!

#include <iostream>
using namespace std;

class State {
public:
	virtual State * a() = 0;
	virtual State * b() = 0;

	virtual bool valid() {
		return false;
	}
};

class State1 : public State {
	State * a() {
		return new State3;
	}
	State * b(){
		return new State2;
	}
	bool valid() {
		return true;
	}
};

class State2 : public State {
	State * a() {
	    return new State4;
	}
	State * b() {
		return new State1;
	}
};

class State3 : public State {
	State * a() {
		return new State1;
	}
	State * b(){
		return new State4;
	}
};

class State4 : public State {
	State * a() {
	    return new State2;
	}
	State * b() {
		return new State3;
	}
};

class StateMachine {
public:
	State * pS;
	State():pS(new State1){};
	
	void a(){
		State * pTemp = pS;
		pS = pTemp->a();
		delete pTemp;
	}
	void b(){ 
		State * pTemp = pS;
		pS = pTemp->b();
		delete pTemp;
	}
	~State(){
		delete pS;
	}
};

int main () {
    State1 S;
	State * pS = &S;

	pS->a();
	
	{
		const int size = 4;
		char orders[size] = {'a', 'a', 'b', 'b'};
		StateMachine FSA;
		
		for(int i = 0; i < size; i++) {
			switch orders[i]{
			case 'a':
				FSA->a();
				break;
			case 'b':
				FSA->b();
				break;
			}
		}
		
		if(! FSA->valid() ) {
			std::cout << "FSA did not accept valid string 'aabb'!"
		}
	}
	
	{
		const int size = 6;
		char orders[size] = {'a', 'b', 'a', 'b', 'a', 'b'}
		StateMachine FSA;
		
		for(int i = 0; i < size; i++) {
			switch orders[i]{
			case 'a':
				FSA->a();
				break;
			case 'b':
				FSA->b();
				break;
			}
		}
		
		if( FSA->valid() ) {
			std::cout << "FSA accepted invalid string 'ababab'!"
		}
	}
	
	{
		const int size = 8;
		char orders[size] = {'a', 'a', 'b', 'a', 'b', 'a', 'b', 'b'}
		StateMachine FSA;
		
		for(int i = 0; i < size; i++) {
			switch orders[i]{
			case 'a':
				FSA->a();
				break;
			case 'b':
				FSA->b();
				break;
			}
		}
		
		if(! FSA->valid() ) {
			std::cout << "FSA did not accept valid string 'aabababb'!"
		}
	}
	
	//Get a string from the user
	//Do the same kind of thing as in the test cases
	
	return 0;
}

In line 16 and 19, you are using State2 and State3, respectively, without defining State2 and State3 previously. At that line, the compiler doesn't know that State2 and State3 is. So it spits out an error.

This article has been dead for over six months. Start a new discussion instead.