#include<iostream>
using namespace std;
#include<string>
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student stu[5];
};
//通过函数给每个老师及所带的学生赋值
void InitTeacher(teacher &s,string t1,student arr[5] )
{
s.name = t1;
for (int i; i < 5; i++){
s.stu[i].name = arr.stu[i].name;
· s.stu[i].score = arr.stu[i].score;
}
}
这是错误提示
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0153 表达式必须具有类类型,但它具有类型 "student *" Project1
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0135 class "student" 没有成员 "stu" Project1
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
struct student {
string name;
int score;
};
struct teacher {
string name;
struct student stu[5];
};
//通过函数给每个老师及所带的学生赋值
void InitTeacher(struct teacher &s,string t1,struct student arr[])
{
s.name = t1;
for (int i=0; i < 5; i++){
s.stu[i].name = arr[i].name;
s.stu[i].score = arr[i].score;
}
}
int main()
{
srand((unsigned int)time(NULL));
struct student Stu[5];
struct teacher tea;
int i;
for(i=0;i<5;i++){
Stu[i].name = rand()%26+65;
Stu[i].score = rand()%100;
}
InitTeacher(tea,"abcd",Stu);
for(i=0;i<5;i++)
cout<<tea.name<<" "<<tea.stu[i].name<<" "<<tea.stu[i].score<<endl;
system("pause");
return 0;
}
void InitTeacher(struct teacher &s,string t1,struct student arr[5] )
我是打算
1.在主函数中赋值好学生数组
2.将学生数组传入InitTeacher函数
3.在函数里for循环对teacher的数组stu成员进行赋值,提高函数的通用性
可是报错,这要如何解决啊?