Error: Requesting Member in Non-Class Type 'Non-Class Type'
When declaring objects in C , it's crucial to use the correct syntax for both constructors. In the provided code, an attempt to create an object using the no-argument constructor results in an error:
Foo foo2();
This syntax is incorrect as it resembles a function declaration, causing the compiler to interpret it as such. The correct syntax for object declaration using the no-argument constructor is:
Foo foo2;
By removing the parentheses, the compiler will correctly recognize foo2 as an object of class Foo.
Alternatively, if you wish to use parentheses for clarity, ensure that the constructor arguments are included:
Foo foo2(1); // Explicitly pass the default argument of 1 to the no-argument constructor
The above is the detailed content of C Constructor Syntax: Why Does `Foo foo2();` Cause an Error?. For more information, please follow other related articles on the PHP Chinese website!