[英]Classes: compiler error «no matching function for call to “constructor”»
I'm new to C++ (and stackoverflow, also). 我是C ++的新手(也包括stackoverflow)。 Unfortunately I don't personally know anyone who knows C++, thus the following question may be too simple to answer.
不幸的是,我个人不认识C ++,因此以下问题可能太简单了。 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. 所以,基本上,我正在尝试链接3个班级:课程,老师,学生。 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()".
但是,当我在Course内部创建Teacher对象时,会弹出一个编译器错误:“没有匹配的函数可调用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;
}
You have explicitly defined a constructor for Teacher
, meaning that is the only constructor that exists. 您已经为
Teacher
明确定义了一个构造函数,这意味着它是唯一存在的构造函数。 In order to have a default parameter-less constructor for you to use in Course
, you must first implement one. 为了在
Course
使用默认的无参数构造函数,您必须首先实现一个。
Adding Teacher();
添加
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. 到您的Teacher.h和
Teacher::Teacher(){}
到Teacher.cpp即可解决您的问题,但是您在Course
的Teacher
将需要手动初始化。
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. 编辑:Badr El Hiouel指出,如果您不希望在默认构造函数中执行任何代码,并且使用的是C ++ 11,则只需将
Teacher() = default
添加到Teacher.h,完全从.cpp省略了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.