Resolving "error: request for member '...' in '...' which is of non-class type"
When encountering the error message "error: request for member '...' in '...' which is of non-class type," in C , it often indicates a problem with object instantiation and class member access. To address this issue, let's delve into the provided code and the root cause of the error.
The code defines a class Foo with two constructors: one that takes no arguments and one that takes one argument. The intention is to create objects of Foo using both constructors. However, an error occurs when attempting to create an object using the no-argument constructor.
Upon examination, the issue lies in the following line:
Foo foo2();
The compiler interprets this line as a function declaration with the name 'foo2' and return type 'Foo.' However, the intended purpose is to declare and instantiate an object of class Foo using the default constructor. To resolve this, the syntax should be modified as follows:
Foo foo2;
By simply removing the parentheses, the compiler now correctly interprets the line as an object declaration and instantiation, using the default constructor to initialize the foo2 object.
This modification allows the code to compile and execute as expected. Objects can now be created using both constructors, and member functions can be accessed without issue. Therefore, remember to use proper syntax for object declarations and instantiation to avoid this type of error in C programming.
The above is the detailed content of Why Does 'error: request for member '...' in '...' which is of non-class type' Occur in C and How Can It Be Fixed?. For more information, please follow other related articles on the PHP Chinese website!