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.
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.
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!