嵌入式狂魔 2021-08-24 20:10 采纳率: 33.3%
浏览 53
已结题

想在函数里传入一个数组,可是报错,如何解决


#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

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2021-08-24 21:24
    关注
    
    #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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月10日
  • 已采纳回答 9月2日
  • 创建了问题 8月24日

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义