Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;错误C2533,ctor:构造函数不允许返回类型_C++_Class_Oop_Constructor - Fatal编程技术网

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()
//                  ^

()首先你缺少一个
在类声明之后。我从来都不知道你可以在适当的位置定义一个返回类型。我现在明白了,谢谢