简体   繁体   中英

C++: Error with 'this' keyword

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

Code:

#include <iostream>
#include "HasCredit.h"
using namespace std;


void insertMoney(int money)
{
    cout<<"You inserted: " << money;
    money = money+this->currentContext->getStateParam(Credit);
    this->currentContext->setStateParam(Credit,money);
    cout<< "Total: "<<money<<endl;
    this->currentContext->setState(Has_Credit);
}

Any suggestions onto why i am getting this error will be most appreciated. Cheers.

Edit: the insertMoney method is within a class called HasCredit.

Edit2: member declarations are now made outside of the constructor

Edit3: Added state class declaration

The Class Definition Code is Below:

#include <iostream>
#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);

};

The state class declaration code is shown Below:

#include <iostream>
#include "Transition.h"
using namespace std;

class State: public Transition
{
protected:
StateContext* currentContext;
public:
State(StateContext* Context);
};

The this pointer is only valid inside a class. Your insertMoney function is not declared to be in a class. See http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/ .

In your definiton of insertMoney (in the code, not the class), you do not declare it to be a member of hasCredit . You need to use void hasCredit::insertMoney instead.

You probably want to attach insertMoney to a class. Try ClassName::insertMoney in your definition.

The error is telling you exactly why you're getting it.

The solution is to fix the definition of what you intend to be a member function, so that the compiler knows it is a member function. Like this:

//   vv THIS WAS MISSING
void HasCredit::insertMoney(int money)
{
    ...

Your member declarations shown in the question are also in the wrong place. They need to be inside the class body, but outside the constructor. When overriding virtual member functions, you may want to show that to readers by using the virtual and override keywords. (Note, override only works if you have a new, C++11 compiler. For older compilers, use a comment /* override */ instead)

class HasCredit: public State
{
    HasCredit (StateContext* Context) : State(Context) {  }
    // vv THIS....................CANNOT BE INSIDE HERE ^^
    virtual void insertMoney(int) override;
    ...
};

You probably want some of your members to be public as well.

There are a few things wrong:

class definition:

#include <iostream>
#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);
};

Note that the methods should be declared outside of the constructor definition.

For the implemenation:

#include <iostream>
#include "HasCredit.h"
using namespace std;

void HasCredit::insertMoney(int money)
{
    cout<<"You inserted: " << money;
    money = money+this->currentContext->getStateParam(Credit);
    this->currentContext->setStateParam(Credit,money);
    cout<< "Total: "<<money<<endl;
    this->currentContext->setState(Has_Credit);
}

The method must be qualified with the name of the class... otherwise it would be considered a definition of a function... and a function does not have a this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM