What is wrong with this?

51 views Asked by At

The assignment does not seem to work. Compiler is telling me is can't find the right constructor.

Course::Course(Course& course){
    if(dynamic_cast<ExamAssessment*>(course.assessment) != NULL){
        assessment = new ExamAssessment(*(course.assessment));
    }

This code is inside the copy constructor of a course class

part of the class

class Course{
    char* courseName;
   float fee;

public:
    Assessment* assessment;

Assessment is a base class that has 3 sub classes(ExamAssessment as 1 of them). In The cpp file of ExamAssessment, I have a copy consructor

ExamAssessment::ExamAssessment(ExamAssessment& exam){
    examMark = exam.examMark;
    CalculateGrade();
}

Obviously the parameter in the assignment is wrong but I'm not sure why(new to C++).

1

There are 1 answers

2
nvoigt On BEST ANSWER

You need to save the result of the cast and use it as your constructor parameter. Your course.assessment may be an ExamAssessment behind the scenes, but the datatype is still Assessment and you don't have a constructor using that type.