C++构造函数的实例都与参数列表匹配

C++ no instance of constructor matches the argument list.

本文关键字:参数 列表 构造函数 实例 C++      更新时间:2023-10-16

我无法弄清楚我的代码出了什么问题。我正在尝试从edx完成实验室作业,但是我无法弄清楚为什么我的代码不起作用。作业需要创建学生班级、教师班级和课程班级。我正在尝试找出课程类构造函数,但它不会接受我的教师对象。我使用指针来创建对象,以便为它分配内存空间(我在edx上学到的东西之一,如果我误解了,请告诉我(。所以我尝试在构造函数中使用引用,但它仍然不起作用。这是代码。我使用"未知"作为填充物。

主代码:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Student.h"
#include "Teacher.h"
#include "Course.h"
using namespace std;
int main()
{
Teacher *teach = new Teacher("Jane", "DoeDoe", 25, "Unknown", "Unknown", "Unknown");
Student *stud1 = new Student();
Student *stud2 = new Student("John", "Doe", 19, "Unknown", "Unknown", "Unknown");
Student *stud3 = new Student("Jane", "Doe", 23, "Unknown", "Unknown", "Unknown");
//Method had two postions. First and Last
stud1->setName("Unknown", "Unknown");
stud1->setAge(20);
stud1->setAddress("Unknown");
stud1->setCity("Unknown");
stud1->setPhone("Unknown");
Course *c = new Course("Intermediate C++", teach, stud1, stud2, stud3);

return 0;
}

课程.h

#pragma once
#include <iostream>
#include "Student.h"
#include "Teacher.h"
#include <string>
using namespace std;
class Course {
private:
string name;
Teacher teacher;
Student student1;
Student student2;
Student student3;

public:

Course(string n, Teacher &t, Student &s1, Student &s2, Student &s3);
~Course();

};

当然.cpp

#include "stdafx.h"
#include "Course.h"

Course::Course(string n, Teacher &t, Student &s1, Student &s2, Student &s3)
{
name = n;
teacher = t;
student1 = s1;
student2 = s2;
student3 = s3;
}
Course::~Course()
{
}
Course(string n, Teacher &t, Student &s1, Student &s2, Student &s3);

希望您将引用传递给ts1s2

Course *c = new Course("Intermediate C++", teach, stud1, stud2, stud3);

在这里你通过teach这是一个teacher*.你想

Course *c = new Course("Intermediate C++", *teach, *stud1, *stud2, *stud3);

您可能想阅读有关引用和指针的更多信息