将类实例作为另一个类参数传入

passing in a class instance as another class parameter

本文关键字:参数 另一个 实例      更新时间:2023-10-16
Course newCourse(id,instructor[id2],name,dept);
Course::courseList.insert(std::pair<int,Course>(id,newCourse));

这是我调用课程类构造函数的代码部分。教练[id2]是我认为它可能起作用的方式,但事实并非如此。

Course(int courseId, Instructor instructor, string courseName, string   dept)
:courseId(courseId),instructor(instructor),courseName(courseName),dept(dept)
{
};

这是类定义中的代码片段。正如您所看到的,其中3个参数是int,2个字符串。我知道如何通过这些考试,但我坚持的是教练-教练的论点。

讲师类将每个人的信息存储在一个以int为键的映射中。我从构建课程类中读取的文件使用int将课程与讲师联系起来。我想我会使用int并查看教练图来找出合适的人的名字,但我一直收到未定义的函数错误。

文件示例:

0,0,Science,Dept
the first 0 s the course ID number and the second is the instructor ID number.

编辑:不同的方法似乎是相同类型的调用

问题方法的代码

224             string myText(line);
225             istringstream iss(myText);
226             if (!(iss>>id)) id = 0;
227             iss.ignore(1,',');
228             if (!(iss>>id2)) id2 = 0;
229             cout<<"id: "<<id<<" id2: "<<id2<<endl;
230             Enrollment newEnrollment(Course::courseList[id], Student::studentList[id2]);

构造函数声明:

87         Enrollment(Student student,Course course):student(student),course(course){}

错误:

:在静态成员函数`static int Enrollment::loadEnrollment()中

230:错误:调用`Enrollment::Enrollment(Course&,Student&)'没有匹配的函数

81:错误:候选为:Enrollment::Enrollment(const Enrollment&)

88:错误:注册::注册()

87:错误:注册::注册(学生,课程)

您正试图访问名为instructor的变量上的operator[]。此变量未定义此运算符。这就是导致编译错误的原因。

加载的讲师位于Instructor::instructorList变量中,如从文件加载讲师的代码所示。

要解决此错误,您必须将包含instructor[id2]的行更改为使用列表:

Course newCourse(id,Instructor::instructorList[id2],name,dept);
相关文章: