gpt4 book ai didi

c++ - 操作数 "="和 "!="的 vector 错误迭代器

转载 作者:行者123 更新时间:2023-11-30 01:48:41 24 4
gpt4 key购买 nike

using namespace std;
vector<IDrawable*>::const_iterator itDrawable;
for(itDrawable= scene.getDrawables().begin(); itDrawable!=scene.getDrawables().end();itDrawable++){
IDrawable *drawable =(*itDrawable);
drawable->draw();
}

此代码向我传递了错误:

Description Resource Path Location Type no match for 'operator!=' (operand types are 'std::vector<IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const
IDrawable*>::const_iterator
{aka __gnu_cxx::__normal_iterator<const
IDrawable* const*, std::vector<const IDrawable*> >
}')

Description Resource Path Location Type no match for 'operator=' (operand types are 'std::vector<IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const
IDrawable*>::const_iterator
{aka __gnu_cxx::__normal_iterator<const
IDrawable* const*, std::vector<const IDrawable*> >
}')

我已经查过这些,我应该与 const_iterator 有什么关系?然而我的scene.getDrawables()看起来像:

const std::vector<const IDrawable*>& getDrawables() const {
return drawables;
}

所以迭代器应该是一个 const_iterator 对吗?我不知道必须改变什么...

最佳答案

你的

const std::vector<const IDrawable*>& getDrawables() const

返回对 const IDrawable* vector 的常量引用指针。然而你的

vector<IDrawable*>::const_iterator itDrawable; 

声明一个 const_iterator到不同类型的 vector ( IDrawable* ,而不是 const IDrawable* )。将定义更改为 vector<const IDrawable*>::const_iterator itDrawable;或者简单地使用 auto在你的 for 中声明迭代器循环,

for(auto itDrawable= scene.getDrawables().cbegin(); ...)

关于c++ - 操作数 "="和 "!="的 vector 错误迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30173522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com