开发者

Why is vector<Data*>::iterator valid and vector<Data*>*::iterator not?

开发者 https://www.devze.com 2023-01-09 03:37 出处:网络
I have these three related class members: vector<Frame*>* poolFrames; vector<Frame*>*::iterator frameIterator;

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

error: invalid use of ‘::’ error: expected ‘;’ before ‘frameIterator’

In reference to the middle line, where I define frameIterators. It goes awa开发者_开发问答y when I loose the pointer to the vector and make it a 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?


I see what you were trying to do. You've defined poolFrames as a pointer to a vector. Then you want to define frameIterator as an iterator for poolFrames. Since poolFrames is a pointer, you think you need a special pointer-to-vector iterator, but you're mistaken.

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:

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().


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.


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. A pointer to such a thing does not have iterators.


The type of vector* is a pointer, which has no iterators.


vector<Frame*>* 

Is a type expression but it does not have a member iterator It's kind of like using . on a pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消