开发者

What could be generating the compiler error in this statement to advance an iterator?

The following line generates a compiler error:

std::vector<int>::iterator blah = std::advance(开发者_如何转开发instructions.begin(), x );

where I have declared:

std::vector<int> instructions;
int x;

The error I get is:

error C2440: 'initializing' : cannot convert from 'void' to 'std::_Vector_iterator<_Ty,_Alloc>'.

What element of that statement is of type void?


advance does not return the advanced iterator, it moves the iterator that's passed as a parameter. So your code should read:

std::vector<int>::iterator blah = instructions.begin();
advance(blah, x);


Without looking this up, I'm guessing the advance function returns void, which you are assigning to blah

try: advance(blah, x);, assuming of course you've initialized blah: blah = instructions.begin();


The return value of advance is void and not an vector<int>::iterator. It instead takes the first parameter by reference and advances it.

  • http://www.sgi.com/tech/stl/advance.html


std::advance doesn't return an iterator -- you need to use it more like:

std::vector<int>::iterator blah = instructions.begin();
advance(blah, x);

Or, since vector has random access iterators anyway:

std::vector<int>::iterator blah = instructions.begin()+x;


cplusplus.com tells me that std::advance returns void, hence the problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜