eduardogfma
eduardogfma

Reputation: 103

Classes: compiler error «no matching function for call to "constructor"»

I'm new to C++ (and stackoverflow, also). Unfortunately I don't personally know anyone who knows C++, thus the following question may be too simple to answer. However I wouldn't post it if I could ask a friend to look at my code. I'm really desperate here.

So, basically I'm trying to link 3 classes: Course, Teacher, Student. Teacher and Student classes "live" inside the Course. However, when I create a Teacher object inside Course a compiler error pops up: "no matching function for call to Teacher::Teacher()".

I can not really see any error, and I can't really grasp the compiler message. Please find my code below. I'm sorry for the wall of code, I tried to suppress as much as I could.

I'm open to any comment and criticism.

Thank you very much in advance.

class Teacher
{
    private:
    string fName;
    string lName;
    int age;
    string address;
    string city;
    string phone;

public:
    // Constructor & destructor
    Teacher(string fName, string lName);
    ~Teacher();

    // Setters & getters
    void setName(string fNameIn, string lNameIn);
    void getName();

    void setAge(int ageIn);
    void getAge();

    void setAddress(string addressIn);
    void getAddress();

    void setCity(string cityIn);
    void getCity();

    void setPhone(string phoneIn);
    void getPhone();

    void GradeStudent(); // outputs an appropriate message to the console such as "Student graded"

    void SitInClass(); // outputs "Sitting at front of class" for the teacher and "Sitting in main theater" for the students.
};

Teacher::Teacher(string fNameIn, string lNameIn)
{
fName = fNameIn;
lName = lNameIn;
//age = 0;
}


class Course
{
private:
    string name;
    Student students;
    Teacher teacher;

public:
    // Constructors & destructor
    Course(string nameCourseIn);
    //Course(string nameCourseIn, Student studentVecIn, Teacher teacherIn);
    ~Course();

    // Setters and getters
    void setName(string courseNameIn);
    void getName();

    void setClass(Student studentVecIn);
    void setTeacher(Teacher teacherIn);
};

int main()
{
std::string name = "Intermediate C++";

Course course (name);
course.getName();

return 0;
}

Upvotes: 0

Views: 656

Answers (1)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

You have explicitly defined a constructor for Teacher, meaning that is the only constructor that exists. In order to have a default parameter-less constructor for you to use in Course, you must first implement one.

Adding Teacher(); to your Teacher.h and Teacher::Teacher(){} to Teacher.cpp will solve your problem, however your Teacher within the Course will need to be initialised manually.

EDIT: Badr El Hiouel kindly pointed out that in the case where you do not wish to execute any code in the default constructor, and you're using C++11, you can simply add Teacher() = default to Teacher.h, omitting it from the .cpp altogether.

Upvotes: 1

Related Questions