Re: Problem with pointers and iterators



On 2008-10-06 16:56:31 -0400, Griff Johns <no@xxxxxxxx> said:


MyObjectList::iterator it;
for (it = list.begin(); it != list.end(); ++it) {
// I used to be able to do this:
MyObject* p_obj = it;
Foo(p_obj);

// Or this
Foo(it);

}

But now the error I get is 'cannot convert parameter 1 from
'std::vector<_Ty>::iterator' to 'MyObject *'


Write on the blackboard 100 times: "An iterator is not a pointer."

In VC6 this worked because vector::iterator was a typedef for T*. In later versions that changed. Both forms satisfy the requirements of the standard.

Well, you have an iterator that refers to an object, and you need the address of that object. So there are two steps: get a reference from the iterator, and take its address.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)

.



Relevant Pages

  • Re: defvar affecting captured closure variables ?
    ... by a SRFI-like process than a revisiting of the ANSI process. ... and doesn't conform to the ANSI standard, there are ways you can open ... collectively referred to as "libel" although it is sometimes broken ... reference document, that makes it easier to correlate it with yours ...
    (comp.lang.lisp)
  • Re: The Enigma has been unravled
    ... There's a similarly strange thing that happened with ... parameters passed by reference. ... "iterator adaptors", before folding them into my library. ... to wrap the default RasterIterator within a simple iterator adaptor. ...
    (comp.ai.philosophy)
  • Re: Buffer overflows and asctime()
    ... reference implementation is IMO a defect in the Standard. ... The behavior of asctime() should be well defined when all relevant ... "normal ranges" by means of side effects in a reference implementation ...
    (comp.std.c)
  • Re: goofy frequencies
    ... And I'd like to have the option of phase locking it to some standard ... reference, ... Back in Germany I've used another outfit that had better pricing. ... For super top notch crystals where price was not an objective we used KVG. ...
    (sci.electronics.design)
  • Re: need help deriving from the std::list class
    ... If you want your class to be used by standard algorithms for searching, ... a pointer or a reference to std::list, and have that other function call ... expect std::list (private inheritance disables implicit conversion to ...
    (comp.lang.cpp)