C++ error C2533, ctor : constructors not allowed a return type [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:23:01

问题


I have a class called teacher

 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;
}

I got an error C2533: 'Teacher::{ctor}' : constructors not allowed a return type. where did i go wrong?


回答1:


You did not put a semicolon after the class definition.

This confuses the parser, which now thinks you're writing something like this:

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

Modern GCC (say, 4.9.2) is quite clear about this problem:

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)



来源:https://stackoverflow.com/questions/29591294/c-error-c2533-ctor-constructors-not-allowed-a-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!