2014-10-22 54 views
-1

我使用模板类来定义3D点(称为vec3<T>),然后将一些点存储在向量中。我用typdef来定义vec3<double> as vec3d;带模板类元素的向量上的迭代器

因此,我试图得到我的矢量vector<vec3d>的迭代器,并且在编译过程中出现错误,我不太明白。我认为重要的是要通过引用另一个类的方法来传递vector<vec3d>

这是我的代码:

for(vector<vec3d>::iterator ite=neighboursList.begin(); ite!=neighboursList.end(); ++ite) 

,这是错误消息:

error: conversion from '__gnu_cxx::__normal_iterator<const vec3<double>*, std::vector<vec3<double>, std::allocator<vec3<double> > > >' to non-scalar type '__gnu_cxx::__normal_iterator<vec3<double>*, std::vector<vec3<double>, std::allocator<vec3<double> > > >' requested 

我会很感激,如果有人能发现什么是错的我在做什么。

betaplus

+0

你有传染媒介s的vec3ds?这是嵌套的。如何使用平面存储进行访问? – 2014-10-22 08:40:28

+0

我只有vec3ds的矢量和vec3d类中的一些“重要”方法 – betaplus 2014-10-23 11:28:53

回答

0

使用const的迭代器:

for (std::vector<vec3d>::const_iterator ite=neighboursList.begin(); 
        /* ^^^^^^^^^^^^^^ /* ite!=neighboursList.end(); ++ite) 
{ 
    // ... 
} 

或者更好,使用auto

for (auto ite = std::begin(neighbourList); ite != std::end(neighbourList); ++ite) 
{ 
    // ... 
} 

或者更好,不要使用迭代器:

for (auto const & neighbour : neighbourList) 
{ 
    // ... 
} 
+0

感谢您的回答,const iterator的工作原理。实际上,我将这个向量作为另一个类的方法的const引用。这就是为什么我需要使用const_iterator。 – betaplus 2014-10-23 11:26:52