Expression must have class type error in C++
The expression must have class type is an error that is thrown when the dot(.) operator is used to access an object’s prpperties, is used on pointers to objects.
Dot(‘.’) Operator is basically used for accessing the fields and methods of an object, but when a dot operator is used on the pointer to object in that condition the error “Expression must have class” type is shown.
When the Dot(‘.’) operator is used on a pointer of class types object in that condition Dot(‘.’) Operator tries to find fields and methods of pointer type but in reality, they do not exist and because of that, we get this error.
Below is the code below to illustrate the above error:
How to resolve this error?
To solve the above error, the idea is to initialize the class without using the new operator i.e., instead of initializing object as “className *obj = new className()”, initialize it as “className obj” to access the member function of the class using dot(‘.’) operator. Below is the program to illustrate the same:
Welcome to GeeksforGeeks!