# include<iostream>
using namespace std;
#include<string>
#include<cstdlib>
//time系统时间头文件包含
# include<ctime>
# include<iomanip>
//1.设计英雄结构体
struct hero
{
string name;
int age;
string gender;
};
void sort(struct hero heroArray,int len)
{
for(int j=0;j<len-1;j++)
{
for(int k=0;k<len-j-1;k++)
{
if(heroArray[k].age>heroArray[k+1].age)
{
struct hero temp=heroArray[k];
heroArray[k]=heroArray[k+1];
heroArray[k+1]=temp;
}
}
}
}
void printhero(struct hero heroArray[],len){
for(int g=0;g<len;g++)
{
cout<<"姓名:"<<heroArray[g].name;
cout<<"年龄:"<<heroArray[g].age ;
cout<<"gender"<<heroArray[g].gender;
}
}
int main(){
//2.创建数组存放英雄
struct hero heroArray[5]=
{
{"JYC",18,"男"},
{"JJJ",16,"女"} ,
{"a",12,"girl"} ,
{"b",133,"man"},
{"c",12,"man"},
};
int len=sizeof(heroArray)/sizeof(heroArray[0]);
// for(int i=0;i<len;i++){
// cout<<"姓名"<<heroArray[i].name
// <<" 年龄:"<<heroArray[i].age
// <<" gender:" <<heroArray[i].gender<<endl;
// }
//3.对数组进行排序,按照年龄进行输出
sort(heroArray,len);
//4.将排序后的结构打印出
pringinf(heroArray,len);
return 0;
}
想问一下为什么在定义SORT函数的时候显示 [Error] no match for 'operator[]' (operand types are 'hero' and 'int')啊?