[英]Why is vector<Data*>::iterator valid and vector<Data*>*::iterator not?
I have these three related class members: 我有以下三个相关的班级成员:
vector<Frame*>* poolFrames;
vector<Frame*>*::iterator frameIterator;
vector<vector<Frame*>::iterator>* poolFrameIterators;
When I compile, gcc tells me 当我编译时,gcc告诉我
error: invalid use of '::' error: expected ';'
错误:无效使用'::'错误:预期';' before 'frameIterator'
在'frameIterator'之前
In reference to the middle line, where I define frameIterators. 参考中线,我在其中定义frameIterators。 It goes away when I loose the pointer to the vector and make it a vector::iterator.
当我松开指向矢量的指针并将其设为vector :: iterator时,它消失了。 However, I want them to be pointers.
但是,我希望它们成为指针。 Is there a special way to define the data type that I want, or do I need to use vector::iterator and then dereference?
是否有一种特殊的方法来定义所需的数据类型,还是需要使用vector :: iterator然后取消引用?
I see what you were trying to do. 我知道你在做什么。 You've defined
poolFrames
as a pointer to a vector. 您已将
poolFrames
定义为指向矢量的指针。 Then you want to define frameIterator
as an iterator for poolFrames
. 然后,你要定义
frameIterator
作为一个迭代poolFrames
。 Since poolFrames
is a pointer, you think you need a special pointer-to-vector iterator, but you're mistaken. 由于
poolFrames
是一个指针,因此您认为您需要一个特殊的指针到向量的迭代器,但是您会犯错。
A vector iterator is a vector iterator is a vector iterator, no matter how you managed to refer to the vector in the first place. 向量迭代器是向量迭代器,是向量迭代器,无论您如何首先设法引用向量。 You need
frameIterator
to be a simple iterator: 您需要
frameIterator
是一个简单的迭代器:
vector<Data*>::iterator frameIterator;
To assign a value to that variable, you'll need to dereference your vector pointer, like this: 要将值分配给该变量,您需要取消引用向量指针,如下所示:
frameIterator = poolFrames->begin();
If poolFrames
were a vector instead of a pointer to a vector, you'd use the dot operator instead: poolFrames.begin()
. 如果
poolFrames
是一个向量,而不是指向向量的指针, poolFrames.begin()
使用点运算符: poolFrames.begin()
。
If you want a pointer to an iterator do it this way round: 如果您想要一个指向迭代器的指针,请按以下方式进行操作:
vector<Frame*>::iterator*
The asterisk always follows the type that is pointed to. 星号始终遵循所指向的类型。 The way you have it is pretty much like writing
vector*<Frame*>::iterator
, it just has the asterisk in the wrong place. 您拥有它的方式非常类似于编写
vector*<Frame*>::iterator
,只是在错误的位置加上了星号。
What data type do you actually want? 您实际上想要哪种数据类型?
vector<Frame*>* poolFrames;
is a pointer to a vector of Frame pointers. 是指向框架指针向量的指针。 Do you actually just want a vector of Frame pointers?
您实际上是否只想要框架指针的向量?
In that context, the error makes sense. 在这种情况下,错误是有道理的。 A
vector<Frame*>
has iterators. vector<Frame*>
具有迭代器。 A pointer to such a thing does not have iterators. 指向此类事物的指针没有迭代器。
vector *的类型是一个没有迭代器的指针。
vector<Frame*>*
Is a type expression but it does not have a member iterator
It's kind of like using .
是一个类型表达式,但没有成员
iterator
,有点像使用.
on a pointer. 在指针上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.