title: C++: Error with 'this' keyword
body: <p>I am attempting to create an poptart vending machine program within c++ and i am trying to use the this keyword, however i am always getting an error stating 'this' may only be used inside a nonstatic member function. Below is a part of the code that i am getting one such issue in</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "HasCredit.h"
  using namespace std;
  
  
  void insertMoney(int money)
  {
      cout&lt;&lt;"You inserted: " &lt;&lt; money;
      money = money+this-&gt;currentContext-&gt;getStateParam(Credit);
      this-&gt;currentContext-&gt;setStateParam(Credit,money);
      cout&lt;&lt; "Total: "&lt;&lt;money&lt;&lt;endl;
      this-&gt;currentContext-&gt;setState(Has_Credit);
  }
  </code></pre>
  
  <p>Any suggestions onto why i am getting this error will be most appreciated. Cheers.</p>
  
  <p>Edit: the insertMoney method is within a  class called HasCredit.</p>
  
  <p>Edit2: member declarations are now made outside of the constructor</p>
  
  <p>Edit3: Added state class declaration</p>
  
  <p>The Class Definition Code is Below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "State.h"
  #include "StateContext.h"
  using namespace std;
  
  class HasCredit: public State
  {
      HasCredit (StateContext* Context) : State(Context) {
      }
          void insertMoney(int);
          void MakeSelectionCoating(int);
          void MakeSelectionFilling(int);
          void moneyRejected(void);
          void addPopTarts(int);
          void dispense(void);
  
  };
  </code></pre>
  
  <p>The state class declaration code is shown Below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Transition.h"
  using namespace std;
  
  class State: public Transition
  {
  protected:
  StateContext* currentContext;
  public:
  State(StateContext* Context);
  };
  </code></pre>