不存在从“”到“*”的适当转换函数
#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student stu[5];
};
void swaper(teacher tea[], int len) {
string name[] = { "wang","li","zhao","xiaowang","xiaoli","xiaoming","xiaozhang","xiaogou" };
for (int i = 0; i < len; i++) {
tea[i].name = name[i];
cout << "teacher's name:" << tea[i].name << endl;
for (int j = 0; j < 5; j++) {
tea[i].stu[j].name = name[j + 3];
tea[i].stu[j].score = rand() % 61 + 40;
cout << "student'name:" << tea[i].stu[j].name << "student'score:" << tea[i].stu[j].score << endl;
}
}
}
int main() {
teacher tea[3];
int len=sizeof(tea)/sizeof(tea[0]);
swaper(tea[3], len);
}
40行去掉[3]
加上rand函数的头文件<stdlib.h>,以及把main函数的调用swaper函数的参数的tea[3]改为tea后(swaper函数第一个参数是teacher指针,所以传入tea),发现正常运行,修改如下:
#include<iostream>
#include <stdlib.h>
using namespace std;
#include<string>
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student stu[5];
};
void swaper(teacher tea[], int len) {
string name[] = { "wang","li","zhao","xiaowang","xiaoli","xiaoming","xiaozhang","xiaogou" };
for (int i = 0; i < len; i++) {
tea[i].name = name[i];
cout << "teacher's name:" << tea[i].name << endl;
for (int j = 0; j < 5; j++) {
tea[i].stu[j].name = name[j + 3];
tea[i].stu[j].score = rand() % 61 + 40;
cout << "student'name:" << tea[i].stu[j].name << " , student'score:" << tea[i].stu[j].score << endl;
}
}
}
int main() {
teacher tea[3];
int len=sizeof(tea)/sizeof(tea[0]);
swaper(tea, len);
}
错误信息里面都会指明是哪一行的