error: base operand of ‘->’ has non-pointer type ‘foobar’

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

example:

g++ example3.cpp -o run.example3
example3.cpp: In function ‘int main()’:
example3.cpp:45: error: base operand of ‘->’ has non-pointer type ‘movies_t’

// example3.cpp
struct movie {
  int year;
  std::string title;
};

int main() {
  movie myMovie;
  myMovie.title = "Beyond Rangoon";
  std::cerr << myMovie->title << std::endl; // wrong.
  return 0;
}

The problem here is that the -> operand can only be used with pointers, and not on an object of the type movie.

int main() {
  movie myMovie;
  movie * pmovie;
  pmovie = &myMovie;
  myMovie.title = "Beyond Rangoon";
  std::cerr << pmovie->title << std::endl; // ok because pmovie is a pointer.
  return 0;
}