我在下面的这个功能有问题:
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; }
宣言:@H_404_5@
char* GetPlayerNameEx(int playerid);
sprintf(string,"%s",CPlayer::GetPlayerNameEx(playerid));
现在我的问题是@H_404_5@
解决方法
您不能将这些函数创建为静态(没有大量调整),因为您试图修改特定实例的数据.要解决您的问题:
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));