C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 非静态成员函数的非法调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在下面的这个功能有问题:
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;
}

宣言:

char* GetPlayerNameEx(int playerid);

用法

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

现在我的问题是

删除了个人信息.

如果这与我怀疑它有什么关系,那么这个函数包含在“Class”头(Declartion)中.

此外,我不知道为什么,但我不能让“代码”框正确适合.

解决方法

您不能将这些函数创建为静态(没有大量调整),因为您试图修改特定实例的数据.要解决您的问题:
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(),MAX_PLAYER_Name);
    return Name;
}
// in your usage
CPlayer player;
// ...
sprintf(String,player.GetPlayerNameEx(playerid));

大佬总结

以上是大佬教程为你收集整理的c – 非静态成员函数的非法调用全部内容,希望文章能够帮你解决c – 非静态成员函数的非法调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。