我需要创建一个结构学生,该学生将嵌套另一个称为课程的结构。然后在“课程”结构中填写入学人数以及他们的ID和姓名
我不确定如何将结构“课程”属性推回到已经具有结构课程作为矢量的结构学生中
#include <iostream>
#include <string>
#include<vector>
using namespace std;
struct course{
int ID;
string name;
};
struct student{
int ID;
string name;
vector <course> ofcourses;
};
void studentDeclare(student &B1){
int coursecount;
cout <<" Student ID: " <<endl;
cin>>B1.ID;
cout <<" Student name: " <<endl;
cin>>B1.name;
cout <<" How many courses?: " <<endl;
cin >> coursecount;
int TempID;
string TempName;
for(int i = 0; i<coursecount;i++)
{
cout <<" Enter course ID: " <<endl;
cin >> TempID;
B1.ofcourses.ID.push_back[TempID];
cout <<" Enter course name: " <<endl;
string TempName;
cin>>TempName;
B1.ofcourses.name.push_back[TempName];
}
};
int main()
{
student boy;
studentDeclare(boy);
print(boy);
system("pause");
}
答案 0 :(得分:2)
B1.ofcourses.ID.push_back[TempID];
B1.ofcourses.name.push_back[TempName];
不对。
B1.ofcourses
是std::vector<course>
。它没有名为ID
或name
的成员。
您需要构造一个course
对象并将其推送到B1.ofourses
。
for(int i = 0; i<coursecount;i++)
{
course c;
cout <<" Enter course ID: " <<endl;
cin >> c.ID;
cout <<" Enter course name: " <<endl;
cin >> c.name;
B1.ofcourses.push_back(c);
}