C++

C++ Error: Expression Must Have A Class Type

Whenever the dot(.) operator is used to retrieve an object’s variables on pointers to objects, the error “expression must have a class type” is raised. If the dot(.) operator is applied to a pointer of any object of the required class, the dot(.) operator attempts to locate the data members and the member functions of the pointer. But they would not exist, which leads to this error.It is possible that we will get an error stating that the “C++ expression must have a class type” when accessing a class function in C++. If we have any pointer of the class, we should be using the arrow(->) operator. When we have an object of the class, we will have to utilize the dot(.) operator to fix the error “expression must have a class type”.

Utilize the Dot(.) Operator

The C++ program to show the “expression must have a class type” error is as follows:

#include <iostream>

using namespace std;
class travel {
public:
    void showMyCountry()
    {
cout<< "I want to go Turkey!";
    }
};
int main()
{

    travel* a = new travel();
a.showMyCountry();  
    return 0;
}

In this program, we introduced the header file <iostream> and also utilized a standard namespace. We declared a class named “travel”. And then defined the function void “showMyCountry()” of this class and set it to public. For this function, the “cout” command is applied to print the message, “I want to go Turkey!”.

We started the coding in the body of the main() function. The object of the class was constructed and we set that object equal to the pointer of the class. We also defined the member function “showMyCountry()” of the class “travel” with the help of the dot(.) operator. In the end, we entered the “return 0” to end the program.

How to Fix the Error “Expression Must Have A Class Type”

The main focus is to declare the required class without the use of the new operator. For instance, rather than declaring the object of the class as “travel *a = new travel(),” declare it as “travel a” to get the class member function with the help of the dot(.) operator.

#include <iostream>

using namespace std;

class travel {
public:
    void showMyCountry()
    {
cout<< "I want to go Turkey!";
    }
};
int main()
{
   travel a;
a.showMyCountry();  
    return 0;
}

First of all, we included header file <iostream>. The standard namespace can also be utilized. We created the “travel” class. The function of this class void “showMyCountry()” function is defined and made public. The “cout” command was applied to show the statement “I want to go Turkey!” within the function. Then, we called the main() function.  The object of the “travel” class was built. We used the dot(.) operator to declare the member function “showMyCountry()” of the “travel” class . Lastly, we typed the “return 0” to exit the program.

Declare the Pointer of the Class

The subsequent code intends to use the dot(.) operator on an object pointer instead of the object directly.

#include <iostream>

using namespace std;
class Language{
 public:
  void func(){
cout<<"Artificial Langugae";
  }    
};
int main()
{
    Language *ptr = new Language();
ptr.func();  
    return 0;
}

At the start of the code, we integrated the <iostream> library for input and output functionalities. Then, we entered the standard namespace. We constructed the “Language” class publicly. We defined the function void func() for this class. We want to show the phrase “Artificial Intelligence” so we used the “cout” statement.

The body of the main() function begins. There, we declared a pointer to the “Language” class. We set the pointer equal to the object of this class. We utilizied the dot(.) operator to call the function func() of the class. However, we declared the pointer “ptr” of this class. Rather than being an element of the pointer to the class object, “func” is a component of the object of “Language” class. To terminate the program, we included the “return 0” in the code.

We are going to utilize the class pointer in the following example. To do this, we’ll insert the arrow(->) operator rather than the dot(.) operator.

#include <iostream>

using namespace std;
class Language
{
public:
void func() {
cout<<"Artificial Intelligence"<func();
return 0;
}
int main()
{
Language *ptr = new Language();
ptr->func();
return 0;
}

After introducing the <iostream> library, we utilized the standard namespace. We publicly defined the “Language” class. For this class, we created the void func() function. We used the “cout” declaration to show the phrase “Artificial Intelligence”. The “endl” command is also used to shift the mouse to the next program line. The body of the main() function starts below:

A pointer to the “Language” class is declared. Then, we put the pointer equivalent to the class object. We invoked the function with the help of class pointer “ptr” and the arrow(->) operator. The “return 0” command was inserted in the code to terminate the program.

Conclusion

In this article, we talked about how the error “expression must have a class type” occurs and how to fix it in C++. When the dot(.) operator, which is generally used to acquire an object’s methods and attributes, is applied on pointers to classes, then the error “expression must have a class type” is returned. The dot(.) operator tries to identify the attributes or functions of a pointer to an object but fails. This is because they do not occur. Instead of being a member of the pointer to a class, these attributes or functions are part of the class itself. As a result, whenever we use a class pointer, we must insert the arrow(->) operator to invoke the function.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content