Home > Backend Development > C++ > Why Does 'request for member '...' in '...' which is of non-class type' Occur When Using C Classes?

Why Does 'request for member '...' in '...' which is of non-class type' Occur When Using C Classes?

Linda Hamilton
Release: 2024-11-30 20:19:11
Original
820 people have browsed it

Why Does

Understanding the Error: "request for member '...' in '...' which is of non-class type"

When attempting to utilize classes and their member functions, the error "request for member '...' in '...' which is of non-class type" often arises. To comprehend its cause, it's crucial to parse the source code that triggered it.

In the provided example, the class Foo possesses two constructors: one that accepts no arguments and another that accepts an integer argument. When instantiating objects using the constructor with the argument, the execution proceeds as anticipated. However, utilizing the no-argument constructor results in the aforementioned error.

Resolving the Issue

The error originates from an incorrect syntax. To rectify it, the following code snippet should be employed:

Foo foo2();
Copy after login

Replace the code above with:

Foo foo2;
Copy after login

The compiler mistakenly interprets the syntax

Foo foo2()
Copy after login

as the declaration of a function named 'foo2' with the return type 'Foo'. However, this is not the intended functionality.

Avoiding Ambiguity

After correcting the syntax, another possible error may surface, reading "call of overloaded 'Foo()' is ambiguous." This occurs because the compiler detects multiple overloaded constructors for Foo. To resolve this, explicitly specify the constructor to be utilized, as shown below:

Foo foo2 = Foo();
Copy after login

The above is the detailed content of Why Does 'request for member '...' in '...' which is of non-class type' Occur When Using C Classes?. 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