Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I call a method of an object in a list iterator in C++?

I have a list of Point objects in C++11 and I want to combine each point’s string value into a longer string. I have a method to get the point as a string in a certain format, but I can’t figure out how to call that method while iterating through the list.

This is my code:

  list<Point> points;
  string results = "";
  
  for (int i = 0; i < num_points; i++) {
    Point ptemp = Point(random01(), random01());
    points.push_back(ptemp);
    results += ptemp.getStr();
  }
  
  string log_output = "";
  string sep = ",";
  list<Point>::iterator iter;
  for (iter = points.begin(); iter != points.end(); ++iter) {
    // get the first three points in the log.txt format
    log_output += *iter.getRawStr(",") + " , ";  
  }

I’m getting this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: ‘std::__cxx11::list<Point>::iterator’ {aka ‘struct std::_List_iterator<Point>’} has no member named ‘getRawStr’
  177 |     log_output += *iter.getRawStr(",") + " , ";

I’ve tried taking away the asterisk, using to_string() instead, and reformatting the loop to use an increasing integer i with the iterator methods called separately inside the loop, but it still doesn’t work. I’ve also tried researching it, but I’ve only seen cout << *iter, not how to specifically call methods from the iterator. What should I do?

>Solution :

It’s a case of operator precedence. The . binds more tightly than *, so this

*iter.getRawStr(",")

is actually this

*(iter.getRawStr(","))

That is, it tries to call getRawStr on the iterator (not the thing it’s pointing to), and then dereference the result. You want

(*iter).getRawStr(",")

which is equivalent[1] to

iter->getRawStr(",")

[1] Technically, operator -> can be overridden, so a pathological class might define it to do something different than unary *, but no normal, sane C++ class should do this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading