C ++错误C2533,ctor:构造函数不允许返回类型

时间:2015-04-12 15:44:43

标签: 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();
}
//This is my constructor
Teacher::Teacher()
{
     ID = 0;
     qualification =" " ;
     salary=0.0;
}

我收到错误C2533:'Teacher :: {ctor}':构造函数不允许返回类型。 我哪里出错了?

1 个答案:

答案 0 :(得分:11)

你没有在课程定义后添加分号。

这会让解析器感到困惑,现在认为你正在写这样的东西:

 class {}     functionName(args) {}
 ^^^^^^^^     ^^^^^^^^^^^^
return type   constructors
 defined     are functions, but
 in-place     they don't have
  (oops)       return types!
                 (oops)

现代海湾合作委员会(比如说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()
//                  ^

live demo