最令人头疼的解析错误:没有参数的构造函数 [英] A most vexing parse error: constructor with no arguments
问题描述
我正在使用g ++在Cygwin中编译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;
我可以发誓我以前在C ++中用括号完成了空的构造函数声明.这可能是我正在使用的编译器的限制吗?还是语言标准真的说不对没有参数的构造函数使用括号?
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?
推荐答案
尽管 MyClass myObj();
可以被解析为带有空初始化程序或函数声明的对象定义,语言标准指定该声明为始终可以通过函数声明解决歧义.在其他情况下,例如,允许使用空括号初始化程序.在 new
表达式中,或构造一个 value-initialized 临时文件.
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 a new
expression or constructing a value-initialized temporary.
这篇关于最令人头疼的解析错误:没有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!