【问题标题】:C++ error C2533, ctor : constructors not allowed a return type [closed]C ++错误C2533,ctor:构造函数不允许返回类型[关闭]
【发布时间】:2015-06-17 22:26:47
【问题描述】:

我有一个班级叫老师

 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}':构造函数不允许返回类型。 我哪里做错了?

【问题讨论】:

  • 首先你在类声明之后缺少;

标签: c++ class oop constructor


【解决方案1】:

您没有在类定义后放置分号。

这会使解析器感到困惑,它现在认为您正在编写如下内容:

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

(live demo)

【讨论】:

  • 我从不知道您可以就地定义返回类型。
  • 我明白了,谢谢
猜你喜欢
  • 2021-12-15
  • 2015-06-05
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多