嵌入式狂魔 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日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度