最令人烦恼的解析错误:没有参数的构造函数

发布于 2024-08-22 05:25:38 字数 423 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫九 2024-08-29 05:25:38

虽然 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 a new expression or constructing a value-initialized temporary.

梨涡少年 2024-08-29 05:25:38

这称为最令人烦恼的解析问题。当解析器看到

MyClass myObj();

它时,它认为您正在声明一个名为 myObj 的函数,该函数没有参数并返回 MyClass

要解决它,请使用:

MyClass myObj;

This is called the Most Vexing Parse issue. When the parser sees

MyClass myObj();

It thinks you are declaring a function called myObj that has no parameters and returns a MyClass.

To get around it, use:

MyClass myObj;
逆光飞翔i 2024-08-29 05:25:38

我在 C++ 标准(第 8.5.8 节)中找到了这一点:

如果对象的初始值设定项是一组空括号,即 (),则应进行值初始化。

[注意:由于 () 不允许
初始化程序的语法,

X a ();

不是类X的对象的声明,而是
声明一个不带参数的函数
参数并返回 X。形式
() 在某些其他情况下是允许的
初始化上下文(5.3.4、5.2.3、
12.6.2)。 ——尾注]

I found this in the C++ standard (§8.5.8):

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

[Note: since () is not permitted by
the syntax for initializer,

X a ();

is not the declaration of an object of class X, but the
declaration of a function taking no
argument and returning an X. The form
() is permitted in certain other
initialization contexts (5.3.4, 5.2.3,
12.6.2). —end note ]

终遇你 2024-08-29 05:25:38

这是一个相当众所周知的问题,并且不依赖于编译器。本质上,您声明了一个返回类型 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.

柠栀 2024-08-29 05:25:38
MyClass myObj();

这被解析为函数声明。该函数名为 myObj,不带参数并返回一个 MyClass 对象。我从未见过编译器接受这一点。另一方面, MyClass* myPtr = new MyClass(); 是可以接受的,这可能会让您感到困惑吗?

MyClass myObj();

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?

千秋岁 2024-08-29 05:25:38

您的行使编译器认为您正在声明一个名为 myObj 的函数,该函数不接受任何参数并返回 MyClass。这种模棱两可的解决方式确实很烦人。

Your line makes the compiler think you are declaring a function named myObj which takes no arguments and returns a MyClass. This ambiguity resolution is indeed annoying.

自由如风 2024-08-29 05:25:38

该标准不需要括号。

int* x = new int;

是合法的语法。

在您的情况下 myclass myobj(); 是一个函数原型。而 myclass myobj; 是一个变量。

The standard does not require parentheses.

int* x = new int;

is legal syntax.

In your case myclass myobj(); is a function prototype. Whereas myclass myobj; is a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文