Q:

Getting undefined class type error but I did create the class and defined it

Im working on an assignment for one of my classes. Simply I have a GumballMachine class and a bunch of State classes that change the state of the GumballMachine.
Here is the offending code:
class GumballMachine;

class State {
public:
 virtual void insertQuarter() const = 0;
 virtual void ejectQuarter() const = 0;
 virtual void turnCrank() const = 0;
 virtual void dispense() const = 0; 

protected:
 GumballMachine *GBM;
};

class NoQuarterState : public State {
public: 
 NoQuarterState (GumballMachine *GBM) {
  this->GBM = GBM;  
 }

 void insertQuarter() const {
  cout << "You inserted a quarter\n";  
  **this->GBM->QuarterInserted();** // <--- C2027 error on MSDN  
 }
};

Now further below I have defined my GumballMachine class as:
class GumballMachine {
public:
 GumballMachine(int numOfGB) {
  this->noQuarterState = new NoQuarterState(this);
  this->soldOutState = new SoldOutState(this);
  this->hasQuarterState = new HasQuarterState(this);
  this->soldState = new SoldState(this);
  this->winnerState = new WinnerState(this);
  this->count = numOfGB;
  if (0 < numOfGB) {
   this->state = this->noQuarterState;
  }
  else {
   this->state = this->soldOutState;
  }  
 }

        ... more code ...

 void QuarterInserted() {
  this->state = this->hasQuarterState;
 }

 ... more code ...

protected:
 int count;
 NoQuarterState *noQuarterState;
 SoldOutState *soldOutState;
 HasQuarterState *hasQuarterState;
 SoldState *soldState;
 WinnerState *winnerState;
 State *state;
};

Visual Studios was throwing a C2259 and C2027 error but after looking at MSDN I feel like I am doing it right. Maybe I am just tired, but I can't seem to find the error/see what I did wrong.
Much thanks to any help. :D

A:

You cannot access any members of GumballMachine before you define the class, so you'll have to either split your file into several files, each containing one class, or define your NoQuarterState::insertQuarter method after the definition of the GumballMachine class:
class NoQuarterState : public State {
  public: 
    NoQuarterState (GumballMachine *GBM) {
       this->GBM = GBM;  
    }

    void insertQuarter() const; // Declaration only
};

class GumballMachine {
  public:
    ...
};

void NoQuarterState::insertQuarter() const {
  cout << "You inserted a quarter\n";  
  this->GBM->QuarterInserted(); // Works now bec. comp. has seen the def.
}

