C++错误 C2533,ctor :构造函数不允许返回类型
C++ error C2533, ctor : constructors not allowed a return type
>我有一个叫老师的班级
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:"老师::{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()
// ^
(现场演示)
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++ - 空模板类构造函数不初始化值
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 为什么复制构造函数不需要检查输入对象是否指向自身?
- 为什么继承的受保护构造函数不能公开?
- 如何确保调用显式构造函数并允许大括号初始化?
- 为什么原始卷曲构造函数 {} 不返回右值?
- 为什么std::atomic的默认构造函数不默认初始化底层存储值
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 如果普通默认构造函数不执行任何操作,为什么我们不能使用 malloc 创建平凡可构造的对象?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?
- 构造函数不允许在匿名聚合中,struct中的字符串
- 继承构造函数不允许类似数组的初始化
- C++错误 C2533,ctor :构造函数不允许返回类型
- 构造函数不允许返回类型(泛型链表)
- 类构造函数:不允许类型不完整(在成员列表中)-VS C++w/QT