加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:11:17 所属栏目:百科 来源:网络整理
导读:我有一个叫老师的课 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 constru
我有一个叫老师的课

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

解决方法

你没有在类定义后加一个分号.

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

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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读