简体   繁体   English

C ++:“ this”关键字错误

[英]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. 我正在尝试在c ++中创建一个poptart自动售货机程序,并且正在尝试使用this关键字,但是我总是收到一条错误消息,指出“ this”只能在非静态成员函数中使用。 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. 编辑:insertMoney方法在名为HasCredit的类中。

Edit2: member declarations are now made outside of the constructor Edit2:现在在构造函数外部进行成员声明

Edit3: Added state class declaration Edit3:添加了状态类声明

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. this指针仅在类内部有效。 Your insertMoney function is not declared to be in a class. 您的insertMoney函数未声明为在类中。 See http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/ . 参见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 . 在您的insertMoney (在代码中,而不是在类中),您没有声明它是hasCredit的成员。 You need to use void hasCredit::insertMoney instead. 您需要改用void hasCredit::insertMoney

You probably want to attach insertMoney to a class. 您可能想将insertMoney附加到类。 Try ClassName::insertMoney in your definition. 在您的定义中尝试ClassName::insertMoney

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. 覆盖虚拟成员函数时,您可能希望通过使用virtualoverride关键字向读者展示这一点。 (Note, override only works if you have a new, C++11 compiler. For older compilers, use a comment /* override */ instead) (注意,仅当您使用新的C ++ 11编译器时, override才有效。对于较旧的编译器,请使用注释/* override */代替)

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. 您可能还希望某些成员也public

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 该方法必须使用类的名称进行限定...否则将被视为函数的定义...并且函数不具有this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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