views:

96

answers:

5

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?

+1  A: 

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

JaredPar
+5  A: 

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);
Jon Benedicto
18 seconds. *shakes fist*
GMan
@GMan:but you still beat me by 2! :-)
Jerry Coffin
And the reason for this, in case anyone cares, is so that it works on Input Iterators, which can't (necessarily) be copied.
Steve Jessop
+1  A: 

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;
Jerry Coffin
+2  A: 

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

+1  A: 

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

Dmitry