我无法弄清楚我的代码有什么问题。我正在尝试从edx完成实验室任务,但我无法弄清楚为什么我的代码无法正常工作。作业需要创建Student类,Teacher类和Course类。我试图找出Course类的构造函数,但它不会接受我的Teacher对象。我使用指针创建对象,以便为内存分配空间(我在edx上学到的东西之一,告诉我,如果我误解了)。所以我尝试在构造函数中使用引用,但它仍然无效。这是代码。我使用" unknown"作为填充物。
主要代码:
#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;
}
Course.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();
};
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()
{
}
答案 0 :(得分:2)
@Component.Invoke()
希望您将引荐传递给Course(string n, Teacher &t, Student &s1, Student &s2, Student &s3);
,t
,s1
..
s2
在这里传递Course *c = new Course("Intermediate C++", teach, stud1, stud2, stud3);
teach
。你想要
teacher*
您可能想要了解有关references and pointers
的更多信息