Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::move_iterator is not a member of 'std'?

29 views
Skip to first unread message

Heinz-Mario Frühbeis

unread,
Apr 27, 2016, 10:04:25 AM4/27/16
to
Hallo,

I was searching for a way to move an iterator to a position in a vector
and found: move_iterator.
E.g.: <http://www.cplusplus.com/reference/iterator/move_iterator/>

So I included:
#include <iostream>
#include <string>
#include <vector>
#include <iterator> // move_iterator

And in a function I wrote:
int main(){
typedef std::vector <std::string> tdVec;
tdVec::iterator it;
tdVec vec;
vec.push_back("Hallo");
vec.push_back("ich");
vec.push_back("bin");
vec.push_back("ein");
vec.push_back("Beispiel");
it = vec.begin();
std::move_iterator<it>(2);
return 0;
}

But this gives me an error:
'move_iterator' is not a member of 'std'

What can I do?

Regards
Heinz-Mario Frühbeis

Cholo Lennon

unread,
Apr 27, 2016, 10:23:45 AM4/27/16
to
'move_iterator' has been available since C++11. Just use a compiler that
at least support C++11.

Regards

--
Cholo Lennon
Bs.As.
ARG

Jens Thoms Toerring

unread,
Apr 27, 2016, 10:32:45 AM4/27/16
to
std::move_iterator is a C++11 feature and the error message
indicates that you tried to compile the progra, without C++11
support enabled. If you're using gcc try to add '-std=c++11'
to the compiler options.

But then you'll get some further error messages: you can't
use 'it' as a template argument, this has to be a type, not
some class instance. And then that's not a function call.

Moreover, a move iterator isn't something that "moves" an
iterator. If you want an iterator to the third element of
the vector 'vec' the just use

it = vec.begin() + 2;

No need for any special kind of object for that - just
add or subtract from an iterator to position it to some-
where else in the object it's iterating over.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Heinz-Mario Frühbeis

unread,
Apr 27, 2016, 10:37:25 AM4/27/16
to
Ok, thanks so far!
But I do not get it work. :(

Here I'm using QT Creator and the 'About QT Creator' shows:
QT Creator 3.0.1
Based on QT 5.2.1 (GCC 4.8.2, 64 bit)

And I made changes in my pro-file like described here:
<http://stackoverflow.com/questions/16948382/how-to-enable-c11-in-qt-creator>

CONFIG += c++11

What else do I have to do?

Regards
Heinz-Mario Frühbeis

Heinz-Mario Frühbeis

unread,
Apr 27, 2016, 10:41:56 AM4/27/16
to
Am 27.04.2016 um 16:32 schrieb Jens Thoms Toerring:
> Heinz-Mario Frühbeis <D...@earlybite.individcore.de> wrote:
[...]
> std::move_iterator is a C++11 feature and the error message
> indicates that you tried to compile the progra, without C++11
> support enabled. If you're using gcc try to add '-std=c++11'
> to the compiler options.
>
> But then you'll get some further error messages: you can't
> use 'it' as a template argument, this has to be a type, not
> some class instance. And then that's not a function call.
>

Ok, please have a look on my answer to Cholo.

> Moreover, a move iterator isn't something that "moves" an
> iterator. If you want an iterator to the third element of
> the vector 'vec' the just use
>
> it = vec.begin() + 2;
>
> No need for any special kind of object for that - just
> add or subtract from an iterator to position it to some-
> where else in the object it's iterating over.
>

Works well. Fine and OK!
Thank you very much...

Regards
Heinz-Mario Frühbeis

David Brown

unread,
Apr 27, 2016, 3:25:45 PM4/27/16
to
Try the correct flag:

-std=c++11

or
-std=gnu++11

(The later form gives you easy access to gcc extensions.)


SG

unread,
Apr 28, 2016, 7:08:37 AM4/28/16
to
On Wednesday, April 27, 2016 at 4:04:25 PM UTC+2, Heinz-Mario Frühbeis wrote:
> Hallo,
>
> I was searching for a way to move an iterator to a position in a
> vector and found: move_iterator.
> E.g.: <http://www.cplusplus.com/reference/iterator/move_iterator/>

That move_iterator probably doesn't do what you think it does.

> typedef std::vector <std::string> tdVec;
> tdVec::iterator it;
> tdVec vec;
> vec.push_back("Hallo");
> vec.push_back("ich");
> vec.push_back("bin");
> vec.push_back("ein");
> vec.push_back("Beispiel");
> it = vec.begin();
> std::move_iterator<it>(2);

First of all "it" is not a type. So, std::move_iterator<it> doesn't
make any sense. Secondly, The move_iterator<> constructor doesn't
take a number. It takes an iterator to wrap.

What you probably meant to write is this:

auto it = vec.begin() + 2;

or this:

auto it = std::next(vec.begin(), 2);

There is also the older std::advance:

auto it = vec.begin();
std::advance(it, 2);

This gives you an iterator that points to "bin". The + operator works
because a vector iterator is a random access iterator and every random
access iterator supports this kind of "iterator arithmetic".

std::advance is a little more general in that it also supports other
iterators (like bidirectional ones).

std::next is the "functional version" of std::advance that is
supported since C++11.

> But this gives me an error:
> 'move_iterator' is not a member of 'std'
>
> What can I do?

move_iterator is also a C++11 feature. So, if you want to make use
of a C++11 feature, you should make sure that the compiler operates
in C++11 mode (or beyond).

Cheers!
SG
0 new messages