我有一堆基于顶点实现几何对象的类,例如 TriangleMesh、PointClouds 或 Edgesets。它们都继承自 VertexBasedGeometry。我现在希望它们都在其顶点的引用上返回一个双向迭代器。这将允许以非模板方式访问任何 VertexBasedGeometry 的顶点。现在,我对迭代器不是很熟悉,这变得相当困难。我的尝试如下所示:
class VertexBasedGeometry : public Geometry
{
public:
typedef std::iterator<std::bidirectional_iterator_tag,defines::Vector3 > VertexIterator;
VertexBasedGeometry(){};
virtual VertexIterator begin()=0;
virtual VertexIterator end()=0;
};
在从 VertexBasedGeometry 继承的 TraingleMesh 中,我现在尝试通过返回包含其顶点的 std::vector 的开始迭代器来实现函数开始。这会在 gcc 4.2 (apple) 上导致以下编译器错误:
Mesh.cpp:25: error: conversion from '__gnu_cxx::__normal_iterator<defines::Vector<double, 3>*, std::vector<defines::Vector<double, 3>, std::allocator<defines::Vector<double, 3> > > >' to non-scalar type 'std::iterator<std::bidirectional_iterator_tag, defines::Vector<double, 3>, long int, defines::Vector<double, 3>*, defines::Vector<double, 3>&>' requested
我现在的问题是:为什么这不起作用,我应该如何改变它才能让它起作用?阅读更多关于迭代器的内容后,我有一种轻微的感觉,即我无法为任何双向迭代器找到通用类型,对吗?一些类可能将它们的顶点存储在 std::vector 以外的容器中,其他类已经提供了(非 stl-conforming )迭代器,我想适应我的常见类型。我愿意接受任何关于如何实施这一点的建议。