将类实例作为另一个类参数传入
passing in a class instance as another class parameter
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);
相关文章:
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 如何在另一个函数中使用返回值作为参数?
- 如果模板参数是另一个模板的实例化,则键入特征测试
- 如何使用另一个类中的公共成员函数作为参数调用线程
- 如何将 lambda 函数作为参数发送到另一个函数
- 将可变参数从一个函数传递到另一个函数
- 如何在C++中提供模板化函数作为另一个函数的参数,默认值?
- 将一个宏传递到另一个宏而不是直接传递内容时会出现不需要的额外"空"参数
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 如何设置默认参数以防用户不输入另一个参数
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 编译器在C++中调用另一个函数时,在参数中查找已删除的构造函数
- 将另一个类的成员函数传递到 std::function 参数中
- 如何参数化用作另一个 lambda 参数的 lambda
- 将参数从一个函数传递到另一个函数(没有模板) - C++
- 重构模板类,该类将其嵌套类用作另一个类的模板参数