我有一堆類實現基於頂點的幾何對象,如TriangleMesh,PointClouds或Edgesets。它們都從VertexBasedGeometry繼承。我現在希望它們都返回一個雙向迭代器對其頂點的引用。這將允許以非模板方式訪問任何VertexBasedGeometry的頂點。現在,我對迭代器不太熟悉,事實證明這很困難。我嘗試如下所示:所有雙向迭代器的常見類類型C++
class VertexBasedGeometry : public Geometry
{
public:
typedef std::iterator<std::bidirectional_iterator_tag,defines::Vector3 > VertexIterator;
VertexBasedGeometry(){};
virtual VertexIterator begin()=0;
virtual VertexIterator end()=0;
};
在TraingleMesh,從VertexBasedGeometry繼承我現在嘗試實現該功能首先返回其中包含了頂點的std ::向量的開始迭代器。這將導致以下編譯器錯誤的GCC 4.2(蘋果):現在
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符合)迭代器,這些迭代器我想適應我的常見類型。我願意接受任何有關如何實施的建議。
我現在就試試看。首先必須閱讀any_pointer。謝謝,@thiton –