The expression must have class type is an error that is thrown when the dot(.) operator is used to access an object’s properties, 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:
// C++ program to illustrate the // Expression must have class // type error #include <iostream> using namespace std;
// Class class GeeksforGeeks {
public :
// Function to display message
void showMyName()
{
cout << "Welcome to GeeksforGeeks!" ;
}
}; // Driver Code int main()
{ // Object of the class
GeeksforGeeks* p = new GeeksforGeeks();
// Member function call
p.showMyName();
return 0;
} |
Output:
// C++ program to illustrate how to // solve Expression must have class // type error #include <iostream> using namespace std;
// Class class GeeksforGeeks {
public :
// Function to display message
void showMyName()
{
cout << "Welcome to GeeksforGeeks!" ;
}
}; // Driver Code int main()
{ // Object of the class
GeeksforGeeks p;
// Member function call
p.showMyName();
return 0;
} |
Welcome to GeeksforGeeks!
Time Complexity: O(1)
Auxiliary Space: O(1)