矢量迭代器不可解除 [英] Vector iterator not dereferencable

查看:306
本文介绍了矢量迭代器不可解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象基类Shape,从中导出Circle和Rectangle,但是当我在VS 2005中执行以下代码时,我得到错误Debug断言失败。同时我没有在任何类中重载==运算符

I have an abstract base class called Shape from which both Circle and Rectangle are derived, but when I execute the following code in VS 2005 I get the error Debug assertion failed. At the same time I have not overloaded == operator in any class

表达式:Vector iterator not dereferencable,这是什么原因。

Expression:Vector iterator not dereferencable, what is the reason for this.

  vector<Shape*> s1;
  s1.push_back(new Circle(point(1,2),3));
  s1.push_back(new Circle(point(4,3),5));
  s1.push_back(new Rectangle(point(1,1),4,5));

  vector<Shape*> s2(s1);
  reverse(s1.begin(),s1.end());

  (*find(s1.begin(),s1.end(),new Circle(point(1,2),3)))->move(point(10,20));


推荐答案

简单:


  • 查找失败,因为无法在向量中找到与之相比的Shape *

  • 失败的find返回结束迭代器

为了像你想要的那样工作,你需要比较Shape,而不是Shape *

For it to work like you want, you do need to compare Shape, not Shape*

正如其他答案所指出的, boost :: ptr_vector 是实现此操作的简单方法。

As pointed out in other answers, boost::ptr_vector is an easy way to achieve this.

这篇关于矢量迭代器不可解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆