Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

illegal call of non-static member function

Tags:

c++

visual-c++

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.

like image 954
user1591117 Avatar asked Aug 22 '13 00:08

user1591117


1 Answers

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:

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.

Like this:

CPlayer myPlayer;
myPlayer.GetPlayerNameEx(playerid);
like image 96
Jiddle Avatar answered Oct 20 '22 03:10

Jiddle