我已经从 Java 转向 CPP,并且正在努力围绕构造构造函数链进行思考,我认为这是我的问题的根源。
我的头文件如下:
public:
GuidedTour();
GuidedTour(string ID, string desc, double fee, int size, string guide, string date)
: Tour(ID, desc, fee);
而我的cpp文件如下:
GuidedTour(string ID, string desc, double fee, int size, string guide, string date)
: Tour(ID, desc, fee) {
this->tourSize = size;
this ->tourGuide = guide;
this -> tourDate = date;
}
我收到两个错误:
guided_tour.h: In constructor 'GuidedTour::GuidedTour(std::string, std::string, double, int, std::string, std::string)':
guided_tour.h:17:25: error: expected '{' at end of input
guided_tour.cpp: At global scope:
guided_tour.cpp:5:19: error: expected ')' before 'ID'
如有任何见解,我们将不胜感激!
最佳答案
要么在头文件中完全定义构造函数,要么只声明它。
换句话说,在头文件中这样做:
GuidedTour(string ID, string desc, double fee, int size, string guide, string date)
: Tour(ID, desc, fee) {
this->tourSize = size;
this ->tourGuide = guide;
this -> tourDate = date;
}
或者在头文件中这样做:
GuidedTour(string ID, string desc, double fee, int size, string guide, string date);
如果你做第一个,那么你当然必须从源文件中删除构造函数。
现在,您可以将它与头文件中的初始化程序列表混合使用。这使得编译器也需要一个函数体。
关于c++ - 预期 { 在输入结束时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18713913/