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
Linda HamiltonOriginal
2024-11-30 20:19:11834browse

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();

Replace the code above with:

Foo foo2;

The compiler mistakenly interprets the syntax

Foo foo2()

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();

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!

Statement:
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