Home > Article > Backend Development > Why Does My C Code Throw an "Error: Request for Member 'function1' in 'myObj', Which is of Non-Class Type 'MyClass ()()' "?
Your encounter with the error message "error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()'" while compiling your C code might have left you perplexed. Let's clarify the confusion surrounding this error.
The issue stems from the ambiguous syntax of an empty constructor declaration with parentheses. While it may appear that the following syntax defines an object with no arguments:
MyClass myObj();
The language standard dictates that such a syntax is actually interpreted as a function declaration. To avoid this ambiguity, the correct syntax for declaring a constructor with no arguments is:
MyClass myObj;
This alternative syntax explicitly denotes an object definition, resolving the ambiguity and allowing the compiler to correctly access the member function function1().
It's not a limitation of the compiler but rather a strict adherence to the C language standard. The standard clearly specifies that a function declaration always takes precedence over an object definition when the syntax is ambiguous.
Therefore, to avoid such errors, ensure that you always use the standard syntax for declaring constructors without arguments:
MyClass myObj;
The above is the detailed content of Why Does My C Code Throw an "Error: Request for Member 'function1' in 'myObj', Which is of Non-Class Type 'MyClass ()()' "?. For more information, please follow other related articles on the PHP Chinese website!