我重新帮你写了个,你看看:
#include
#include
#include
已级给你实现了,我在具体修正了的地方加了注释:
#include "stdafx.h"
#include "stdafx.h"
#include
#include
#include
using namespace std;
class Player{
private:
char PlayerID[20];
char PlayerName[10];
int PlayerLevel;
int Experience;
public:
Player();
~Player();
void GetPlayerInfo();
void PutPlayerinfo(Player *ptr);
int search(Player *ptr, char *ID);
void sort(Player *ptr);
};
Player::Player()//构造函数得要
{
}
Player::~Player()//析造函数也得要
{
}
void Player::GetPlayerInfo(){
int cont=0;
cout<<"\n输入玩家的ID:"<
cout<<"\n输入玩家的姓名:"<
PlayerLevel=rand()%100; //据题意要加个0-100的范围
Experience=cont++;
}
void Player::PutPlayerinfo(Player *ptr){
int cont=0;
while( cont<5 )
{
cout<<" \n玩家ID:"<
}
}
int Player::search(Player *ptr,char *ID){ //这个函数前你忘加Player::
for ( int i=0;i<5;i++ )
{
if ( !strcmp(ptr[i].PlayerID,ID) ) //两个字符串不能直接比较得用函数,关于这个函数请看MSDN
{
cout<<"你要查找的游戏玩家的ID是:"<
}
ptr++;
}
return 0;
}
void Player::sort(Player *ptr){ //这个函数前你忘加Player::
Player Temp;
int i,j;
for (i=0;i<5;i++ ) //一个标准的冒泡排序
{
for (j=i+1;j<5;j++ )
if (ptr[i].PlayerLevel
Temp.Experience=ptr[i].Experience;
strcpy(Temp.PlayerID,ptr[i].PlayerID);
strcpy(Temp.PlayerName,ptr[i].PlayerName);
ptr[i].PlayerLevel=ptr[j].PlayerLevel;
ptr[i].Experience=ptr[j].Experience;
strcpy(ptr[i].PlayerID,ptr[j].PlayerID);
strcpy(ptr[i].PlayerName,ptr[j].PlayerName);
ptr[j].PlayerLevel=Temp.PlayerLevel;
ptr[j].Experience=Temp.Experience;
strcpy(ptr[j].PlayerID,Temp.PlayerID);
strcpy(ptr[j].PlayerName,Temp.PlayerName);
}
}
}
int main()
{
Player P[5];
int i=0;
while ( i<5 )
{
P[i].GetPlayerInfo();
i=i+1;
}
cout<<"请输入要找的玩家ID:"<
cin>>ID;
Player Test;
Test.search( P,ID );
Test.PutPlayerinfo( P ); //参数是指针所以不要用P[0],一定要这么定的话也要写成&P[0]
Test.sort( P );
Test.PutPlayerinfo( P );
system("PAUSE");
return 0;
}
因为类的私有成员变量不能在类的外部访问,你把那些私有成员变量通过成员函数取出来,或者定义为public的。