Iterator for pointer vector, not dereferencing correctly

Here is my problem:

I have std::vector<AguiWidgetBase*>one that is used to track child controls.

I have two functions for returning iterators:

std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChildBeginIterator() const
{
    return children.begin();
}

std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChildEndIterator() const
{
    return children.end();
}

Then I use it as follows:

for(std::vector<AguiWidgetBase*>::const_iterator it = box->getChildBeginIterator(); 
    it != box->getChildEndIterator(); ++it)
{
    it->setText("Hello World");
}

and I get the following errors:

Error   3   error C2039: 'setText' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>'   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\main.cpp   112
Error   2   error C2839: invalid return type 'AguiWidgetBase *const *' for overloaded 'operator ->' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\main.cpp   112

Why does this give me these errors?

thank

+5
source share
3 answers

Is there a way to change my iterators so that it-> works?

Not directly, but you can do something like:

for(std::vector<AguiWidgetBase*>::const_iterator it = box->getChildBeginIterator(); 
    it != box->getChildEndIterator(); ++it)
{
    AguiWidgetBase* p = *it;

    p->setText("Hello World");
}
+4
source

Since the iterator acts like a pointer, in which case a pointer to a pointer.

You will need:

(*it)->setText("Hello World"); // dereference iterator, dereference pointer
+23
source

, , , , , .

You can use boost::ptr_vectorAguiWidgets to collect by pointer, but work with them as if they were saved by value? I have not used it extensively, but my vague memory is that it works that way.

0
source

All Articles