Home > Backend Development > C++ > Why Does My C Code Throw an 'Error: Request for Member 'function1' in 'myObj', Which is of Non-Class Type 'MyClass ()()' '?

Why Does My C Code Throw an 'Error: Request for Member 'function1' in 'myObj', Which is of Non-Class Type 'MyClass ()()' '?

Mary-Kate Olsen
Release: 2024-11-12 12:12:02
Original
394 people have browsed it

Why Does My C   Code Throw an

Confusion Resolved: Understanding the Error "Constructor with No Arguments"

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();
Copy after login

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;
Copy after login
Copy after login

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;
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template