最令人烦恼的解析错误:没有参数的构造函数
我正在使用 g++ 在 Cygwin 中编译 C++ 程序,并且我有一个类,其构造函数没有参数。我有以下几行:
MyClass myObj();
myObj.function1();
当尝试编译它时,我收到消息:
错误:请求“myObj”中的成员“function1”,该成员属于非类类型“MyClass ()()”
经过一番研究,我发现修复方法是将第一行更改为
MyClass myObj;
我可以发誓我已经之前在 C++ 中完成了带括号的空构造函数声明。这可能是我正在使用的编译器的限制,还是语言标准真的说不要在没有参数的构造函数中使用括号?
I was compiling a C++ program in Cygwin using g++ and I had a class whose constructor had no arguments. I had the lines:
MyClass myObj();
myObj.function1();
And when trying to compile it, I got the message:
error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()'
After a little research, I found that the fix was to change that first line to
MyClass myObj;
I could swear I've done empty constructor declarations with parentheses in C++ before. Is this probably a limitation of the compiler I'm using or does the language standard really say don't use parentheses for a constructor without arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
虽然
MyClass myObj();
可以被解析为带有空初始化器或函数声明的对象定义,但语言标准指定始终以有利于函数声明的方式解决歧义。在其他上下文中允许使用空括号初始值设定项,例如在new
表达式中或构造值初始化临时值。Although
MyClass myObj();
could be parsed as an object definition with an empty initializer or a function declaration the language standard specifies that the ambiguity is always resolved in favour of the function declaration. An empty parentheses initializer is allowed in other contexts e.g. in anew
expression or constructing a value-initialized temporary.这称为最令人烦恼的解析问题。当解析器看到
它时,它认为您正在声明一个名为
myObj
的函数,该函数没有参数并返回MyClass
。要解决它,请使用:
This is called the Most Vexing Parse issue. When the parser sees
It thinks you are declaring a function called
myObj
that has no parameters and returns aMyClass
.To get around it, use:
我在 C++ 标准(第 8.5.8 节)中找到了这一点:
I found this in the C++ standard (§8.5.8):
这是一个相当众所周知的问题,并且不依赖于编译器。本质上,您声明了一个返回类型 MyObj 的函数。毫不奇怪,您无法调用它的构造函数。有关详细说明,请参阅 C++ faq lite。
This is a fairly well-known issue and isn't compiler dependent. Essentially, you were declaring a function returning type MyObj. Not surprisingly, you couldn't call its constructor. See the C++ faq lite for a good explanation.
这被解析为函数声明。该函数名为 myObj,不带参数并返回一个 MyClass 对象。我从未见过编译器接受这一点。另一方面,
MyClass* myPtr = new MyClass();
是可以接受的,这可能会让您感到困惑吗?That's parsed as a function declaration. The function is called myObj, takes no arguments and returns a MyClass object. I've never seen a compiler accepting that. On the other hand,
MyClass* myPtr = new MyClass();
is acceptable, and may be that got you confused?您的行使编译器认为您正在声明一个名为
myObj
的函数,该函数不接受任何参数并返回MyClass
。这种模棱两可的解决方式确实很烦人。Your line makes the compiler think you are declaring a function named
myObj
which takes no arguments and returns aMyClass
. This ambiguity resolution is indeed annoying.该标准不需要括号。
是合法的语法。
在您的情况下
myclass myobj();
是一个函数原型。而myclass myobj;
是一个变量。The standard does not require parentheses.
is legal syntax.
In your case
myclass myobj();
is a function prototype. Whereasmyclass myobj;
is a variable.