The error “member reference type is a pointer; did you mean to use ‘->'” occurs when using the dot .
operator on a pointer to an object. We use the dot operator to access an object’s fields and methods.
You can solve the error by using the arrow operator ->
if using a pointer to an object otherwise, apply the dot operator directly to the object.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
Example
Let’s look at an example to reproduce the error.
#include <iostream> // Declare Rectangle class class Rectangle{ // Two data members of type int with private access int width, height; // Two member functions with public access declared public: void set_values(int,int); int area() const { return width * height; } }; // Define set_values function outside of the class void Rectangle::set_values(int x, int y) { width = x; height = y; } int main() { // Create a pointer to object of Rectangle class auto *rec = new Rectangle(); // Attempt to define width and height of rectangle rec.set_values(3, 4); // Attempt to calculate area of rectangle std::cout << "Area: " << rec.area(); return 0; }
Let’s run the code to see what happens:
error: member reference type 'Rectangle *' is a pointer; did you mean to use '->'? rec.set_values(3, 4); ~~~^ ->
The error occurs because we tried to apply the dot operator on a pointer to the Rectangle object rather than on the object itself. We can also encounter the error as “expression must have class type”.
Solution #1: Use ->
We can access members of the object through a pointer using the arrow ->
operator instead of the dot operator. Let’s look at the revised code:
#include <iostream> class Rectangle{ int width, height; public: void set_values(int,int); int area() const { return width * height; } }; void Rectangle::set_values(int x, int y) { width = x; height = y; } int main() { // Create a pointer to object of Rectangle class auto *rec = new Rectangle(); // Define width and height of rectangle using arrow operator rec->set_values(3, 4); // Attempt to calculate area of rectangle using arrow operator std::cout << "Area: " << rec->area(); return 0; }
Let’s run the code to get the result:
Area: 12
We successfully calculated the area of the rectangle.
Solution #2: Access class object instead of pointer to object
We can also solve the error by applying the dot operator directly in the Rectangle object. Let’s look at the revised code:
#include <iostream> class Rectangle{ int width, height; public: void set_values(int,int); int area() const { return width * height; } }; void Rectangle::set_values(int x, int y) { width = x; height = y; } int main() { // Create an object of the Rectangle class Rectangle rec{}; // Set width and height values using dot operator rec.set_values(3, 4); // Calculate area using dot operator std::cout << "Area: " << rec.area(); return 0; }
Let’s run the code to get the result:
Area: 12
We successfully calculated the area of the rectangle.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on C++, go to the articles:
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.