C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C错误C2533,ctor:构造函数不允许返回类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个叫老师的课

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)

大佬总结

以上是大佬教程为你收集整理的C错误C2533,ctor:构造函数不允许返回类型全部内容,希望文章能够帮你解决C错误C2533,ctor:构造函数不允许返回类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。