有代码:
struct B
{
int* a;
B(int value):a(new int(value))
{ }
B():a(nullptr){}
B(const B&);
}
B::B(const B& pattern)
{
}我收到错误消息:
‘错误1错误C2533:'B::{ctor}’:构造函数不允许返回类型‘
知道为什么吗?
附注:我使用的是VS 2010RC
发布于 2010-04-06 00:58:30
您的struct定义后缺少分号。
错误是正确的,构造函数没有返回类型。由于缺少分号,整个结构定义被视为函数的返回类型,如下所示:
// vvv return type vvv
struct { /* stuff */ } foo(void)
{
}添加分号:
struct B
{
int* a;
B(int value):a(new int(value))
{ }
B():a(nullptr){}
B(const B&);
}; // end class definition
// ah, no return type
B::B(const B& pattern)
{
}发布于 2010-04-06 00:59:25
你需要一个更好的编译器。使用g++:
a.cpp:1: error: new types may not be defined in a return type
a.cpp:1: note: (perhaps a semicolon is missing after the definition of 'B')
a.cpp:5: error: return type specification for constructor invalid分号是必需的,因为它终止了可能的结构实例列表:
struct B {
...
} x, y, z;创建三个名为x,y和z的B实例。这是C++C继承的一部分,并将仍然存在于C++0x中。
https://stackoverflow.com/questions/2579657
复制相似问题