Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指针向量的迭代器未正确解引用_C++_Vector_Iterator - Fatal编程技术网

C++ 指针向量的迭代器未正确解引用

C++ 指针向量的迭代器未正确解引用,c++,vector,iterator,C++,Vector,Iterator,这是我的问题: 我有一个std::vector,用于跟踪子控件 我有两个函数返回迭代器: std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChildBeginIterator() const { return children.begin(); } std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChil

这是我的问题:

我有一个
std::vector
,用于跟踪子控件

我有两个函数返回迭代器:

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

std::vector<AguiWidgetBase*>::const_iterator AguiWidgetBase::getChildEndIterator() const
{
    return children.end();
}
std::vector::const_迭代器AguiWidgetBase::getChildBeginIterator()const
{
返回children.begin();
}
std::vector::const_迭代器AguiWidgetBase::getChildEndIterator()const
{
返回子项。end();
}
然后我就这样使用它:

for(std::vector<AguiWidgetBase*>::const_iterator it = box->getChildBeginIterator(); 
    it != box->getChildEndIterator(); ++it)
{
    it->setText("Hello World");
}
for(std::vector::const_迭代器it=box->getChildBeginIterator();
it!=box->getChildEndIterator();++it)
{
it->setText(“你好世界”);
}
我得到了这些错误:

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
错误3错误C2039:“setText”:不是“std::\u Vector\u const\u迭代器”c:\users\josh\documents\visual studio 2008\projects\agui\allge\u 5\main.cpp 112的成员
错误2错误C2839:对于重载的“运算符->”c:\users\josh\documents\visual studio 2008\projects\agui\Allege_5\main.cpp 112,返回类型“AguiWidgetBase*const*”无效
为什么它会给我这些错误


谢谢,因为迭代器的作用类似于指针,在本例中是指向指针的指针

您需要:

(*it)->setText("Hello World"); // dereference iterator, dereference pointer
有没有一种方法可以更改迭代器以使其工作

不是直接的,但你可以这样做:

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

    p->setText("Hello World");
}
for(std::vector::const_迭代器it=box->getChildBeginIterator();
it!=box->getChildEndIterator();++it)
{
AguiWidgetBase*p=*it;
p->setText(“你好世界”);
}

正如其他人所指出的,这是因为向量中存储的对象是指针,因此对实际对象有额外的间接层次


您可以使用
boost::ptr_向量
通过指针收集AguiWidget,但使用它们时,就好像它们是通过值存储的一样?我没有广泛地使用它,但我模糊地记得它是这样工作的。

它不是迭代器。这是因为向量是指针向量。撤销对迭代器的引用会给您一个指向AguiWidgetBase的指针,您必须再次撤销引用才能使用它。@Milo:不容易,不。您可以自己创建迭代器。如果Agui与大多数其他GUI框架一样工作,那么这些小部件就是自毁的。然后使用
共享\u ptr
将是一个非常糟糕的主意。但这取决于Agui,不管它是什么。你是对的,它们在自我毁灭