Home > Backend Development > C++ > Why Does 'error: request for member '...' in '...' which is of non-class type' Occur in C and How Can It Be Fixed?

Why Does 'error: request for member '...' in '...' which is of non-class type' Occur in C and How Can It Be Fixed?

Mary-Kate Olsen
Release: 2024-12-05 06:22:11
Original
823 people have browsed it

Why Does

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();
Copy after login

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;
Copy after login

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!

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