[英]pushing a vector of structures into a structure?
i need to create a structure student that will nest another structure called course.我需要创建一个结构学生,它将嵌套另一个称为课程的结构。 and then fill the structure "course" with how many the student is enrolled in along with their id and name
然后在结构“课程”中填写学生注册的人数以及他们的 ID 和姓名
I am not sure how can i pushback the structure "course" attributes into the structure student which already have the structure course as a vector in it我不确定如何将结构“课程”属性推回已经将结构课程作为向量的结构学生
#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");
}
B1.ofcourses.ID.push_back[TempID];
B1.ofcourses.name.push_back[TempName];
are not right.不对。
B1.ofcourses
is a std::vector<course>
. B1.ofcourses
是一个std::vector<course>
。 It does not have a member named ID
or name
.它没有名为
ID
或name
的成员。
You need to construct a course
object and push it to B1.ofourses
.您需要构建一个
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.