-3

I just dont get what im doing wrong here. The goal is a finite state machine which changes the state and its behaviour depending on the char that it gets, but i can't get even the project structure down... here is the code i got so far, it wont complile, sorry i dont have the error codes, but one was definatley in the constructor of Automat where i tried to set the currentState to initial.

#ifndef Automat_H_
#define Automat_H_

class State;

class Automat {

public:

    Automat();
    virtual ~Automat();
    void setCurrentState(State* newCurrentState);
    void read(char c);

private:
    State* currentState;

};

#endif /* Automat_H_ */


#ifndef STATE_H_
#define STATE_H_

class Automat;

class State {
    virtual ~State();
    virtual void read(char c, Automat* m) = 0;
};

#endif /* STATE_H_ */

#ifndef INITIAL_H_
#define INITIAL_H_

#include "State.h"

class Initial: public State{
    virtual ~Initial();

    void read(char c, Automat* m);
};



#endif /* INITIAL_H_ */


//Automat.cpp

#include "../includes/Automat.h"

Automat::Automat() {
    currentState = new Initial();
}

Automat::~Automat() {
    // TODO Auto-generated destructor stub
}

void Automat::read(char c){
    //currentState->read(c, this);
}
mdshields
  • 37
  • 3
  • 4
    "sorry i dont have the error codes" - so why not ask the compiler what they were? You're more likely to get help if you can narrow the code down to a [minimal test case](http://stackoverflow.com/help/mcve) and tell us exactly what goes wrong. – Mike Seymour Apr 21 '15 at 09:34

1 Answers1

1

Presumably, the missing error messages told you exactly what was wrong. My compiler gives errors like

prog.cpp:29:13: error: 'virtual State::~State()' is private

because all the members of State and Initial are private - that's the default if you introduce a class definition with the class keyword.

Either add public: to these class definitions, or change the keyword to struct.

You'll also need to include "Initial.h" from "Automat.cpp", since it needs the full definition of that class.

Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
  • Thanks for your help, I did change what you said and now im getting this: "undefined reference to vtable for initial" and the same for state. Just now i got rid of the state one by giving the deconstructor a body... that didnt do the job for the Initial... so i think it is the read() method .... do i need to implement this in the header? – mdshields Apr 21 '15 at 10:57
  • @mdshields: Yes, you'll have to implement `Initial::read()` (and, in general, all non-pure virtual functions). You could simply remove the `Initial` destructor, the implicitly generated one is good enough unless you need it to do anything special. The `State` constructor will also need an implementation, you can just give it an empty body in the class. – Mike Seymour Apr 21 '15 at 17:27