C++ C++;错误C2533,ctor:构造函数不允许返回类型
我有一个班叫老师C++ C++;错误C2533,ctor:构造函数不允许返回类型,c++,class,oop,constructor,C++,Class,Oop,Constructor,我有一个班叫老师 class Teacher { private: int ID; string qualification; double salary; Date DOB; Date dateJoined; public: Teacher(); void setTeacher (int, string, double); string getQualification(); void displayTeacher();
class Teacher
{
private:
int ID;
string qualification;
double salary;
Date DOB;
Date dateJoined;
public:
Teacher();
void setTeacher (int, string, double);
string getQualification();
void displayTeacher();
}
//This is my constructor
Teacher::Teacher()
{
ID = 0;
qualification =" " ;
salary=0.0;
}
我得到一个错误C2533:“教师:{ctor}”:构造函数不允许返回类型。
我哪里出错了?您没有在类定义后加分号 这让解析器感到困惑,它现在认为您正在编写以下内容:
class {} functionName(args) {}
^^^^^^^^ ^^^^^^^^^^^^
return type constructors
defined are functions, but
in-place they don't have
(oops) return types!
(oops)
现代GCC(如4.9.2)非常清楚这个问题:
class Teacher
{
Teacher();
}
Teacher::Teacher()
{}
// main.cpp:3:1: error: new types may not be defined in a return type
// class Teacher
// ^
// main.cpp:3:1: note: (perhaps a semicolon is missing after the definition of 'Teacher')
// main.cpp:8:18: error: return type specification for constructor invalid
// Teacher::Teacher()
// ^
()首先你缺少一个
代码>在类声明之后。我从来都不知道你可以在适当的位置定义一个返回类型。我现在明白了,谢谢