C++ 非法调用非静态成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18369972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:56:13  来源:igfitidea点击:

illegal call of non-static member function

c++visual-c++

提问by user1591117

I'm having trouble with this function below:

我在使用下面的这个功能时遇到了问题:

char* GetPlayerNameEx(int playerid)
{

    char Name[MAX_PLAYER_NAME], i = 0;

    GetPlayerName(playerid, Name, sizeof(Name));

    std::string pName (Name);

    while(i == 0 || i != pName.npos)
    {
        if(i != 0) i++;
        int Underscore = pName.find("_", i);
        Name[Underscore] = ' ';
    }
    return Name;
}

declaration:

宣言:

char* GetPlayerNameEx(int playerid);

usage:

用法:

sprintf(string, "%s", CPlayer::GetPlayerNameEx(playerid));

Now my problem here is

现在我的问题是

Removed personal information.

删除了个人信息。

If this has anything to do whith it which I doubt it does, this function is contained within a "Class" header (Declartion).

如果这与我怀疑它有任何关系,则此函数包含在“类”标头(声明)中。

Also I have no idea why but I can't get the "Code" box to fit over correctly.

我也不知道为什么,但我无法让“代码”框正确地适应。

采纳答案by Zac Howland

You cannot create these functions as static (without a lot of tweaking) because you are attempting to modify the data of a specific instance. To fix your problem:

您不能将这些函数创建为静态(无需大量调整),因为您正在尝试修改特定实例的数据。要解决您的问题:

class CPlayer
{
public:
    // public members

    // since you are operating on class member data, you cannot declare these as static
    // if you wanted to declare them as static, you would need some way of getting an actual instance of CPlayer
    char* GetPlayerNameEx(int playerId);
    char* GetPlayerName(int playerId, char* name, int size);
private:
    // note:  using a std::string would be better
    char m_Name[MAX_PLAYER_NAME];
};

// note:  returning a string would be better here
char* CPlayer::GetPlayerNameEx(int playerId)
{
    char* Name = new char[MAX_PLAYER_NAME];
    memset(Name, MAX_PLAYER_NAME, 0);
    GetPlayerName(playerId, m_Name, sizeof(m_Name));
    std::string sName(m_Name);
    std::replace(sName.begin(), sName.end(), '_', ' ');
    ::strncpy(sName.c_str(), Name, MAX_PLAYER_NAME);
    return Name;
}
// in your usage
CPlayer player;
// ...
sprintf(string, "%s", player.GetPlayerNameEx(playerid));

回答by Jiddle

Illegal call of non-static member function means that you are trying to call the function without using an object of the class that contains the function.

非法调用非静态成员函数意味着您试图在不使用包含该函数的类的对象的情况下调用该函数。

The solution should be to make the function a static function.

解决方案应该是使函数成为静态函数。

This is normally what causes the error C2352:

这通常是导致错误 C2352 的原因:

class MyClass {
    public:
        void MyFunc() {}
        static void MyFunc2() {}
};

int main() {
    MyClass::MyFunc();   // C2352
    MyClass::MyFunc2();   // OK
}

If making it static is not an option for you, then you have to create an instance of the class CPlayer.

如果使它成为静态不是您的选择,那么您必须创建类 CPlayer 的实例。

Like this:

像这样:

CPlayer myPlayer;
myPlayer.GetPlayerNameEx(playerid);

回答by Lochemage

CPlayer::GetPlayerNameEx(playerid)

You can't use the scope (::) operator on a class type to call a function unless it is a static function. To call a function on an object, you actually have to create the memory for that object first (via making a CPlayervariable somewhere) and then calling the function on that object.

您不能::在类类型上使用作用域 ( ) 运算符来调用函数,除非它是静态函数。要在对象上调用函数,您实际上必须首先为该对象创建内存(通过在CPlayer某处创建变量),然后在该对象上调用该函数。

Static functions are global and specifically do not mess with member variables of the class (unless they are also static) which makes them valid to call without the scope of an actual object instance.

静态函数是全局的,特别是不会与类的成员变量混淆(除非它们也是静态的),这使得它们可以在没有实际对象实例范围的情况下有效调用。