Reputation: 61
I cannot figure out what is wrong with my code. I am trying to finish a lab assignment from edx, but I cannot figure out why my code won't work. The assignment requires to create a Student class, Teacher class, and Course class. I am trying to figure out the Course class Constructor, but it will not accept my Teacher object. I use a pointer to create the objects so that I allocate space in memory for it (one of the things I learned on edx, tell me if I misunderstood). So I tried to use reference in the constructor, but it still did not work. Here is the code. I use "unknown" as filler.
Main code:
#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()
{
}
Upvotes: 1
Views: 16459
Reputation: 7542
Course(string n, Teacher &t, Student &s1, Student &s2, Student &s3);
expects you to pass references to t
,s1
, s2
..
Course *c = new Course("Intermediate C++", teach, stud1, stud2, stud3);
Here you pass teach
which is a teacher*
. You want to
Course *c = new Course("Intermediate C++", *teach, *stud1, *stud2, *stud3);
You might want to read more about references and pointers
Upvotes: 3