A most vexing parse error: constructor with no arguments

C++ConstructorStandards

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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;

Solution 3 - C++

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 ]

Solution 4 - C++

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.

Solution 5 - C++

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?

Solution 6 - C++

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.

Solution 7 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmringView Question on Stackoverflow
Solution 1 - C++CB BaileyView Answer on Stackoverflow
Solution 2 - C++Peter AlexanderView Answer on Stackoverflow
Solution 3 - C++suszterpattView Answer on Stackoverflow
Solution 4 - C++Liz AlbinView Answer on Stackoverflow
Solution 5 - C++sbkView Answer on Stackoverflow
Solution 6 - C++AriView Answer on Stackoverflow
Solution 7 - C++JonHView Answer on Stackoverflow