gpt4 book ai didi

C++ 没有构造函数的实例与参数列表匹配。

转载 作者:行者123 更新时间:2023-11-28 01:32:28 26 4
gpt4 key购买 nike

我无法弄清楚我的代码有什么问题。我正在尝试完成 edx 的实验室作业,但我无法弄清楚为什么我的代码无法运行。该作业需要创建一个 Student 类、Teacher 类和 Course 类。我正在尝试找出 Course 类构造函数,但它不会接受我的 Teacher 对象。我使用一个指针来创建对象,以便为它分配内存空间(我在 edx 上学到的东西之一,如果我误解了请告诉我)。于是尝试在构造函数中使用reference,还是不行。这是代码。我使用“未知”作为填充词。

主要代码:

#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;
}

类(class).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();


};

类(class).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);

您可能想阅读有关 references and pointers 的更多信息

关于C++ 没有构造函数的实例与参数列表匹配。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50893413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com