Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Problem programmer?

Name: Anonymous 2011-04-20 14:49

in main:

vector<int> player_nr;
int x = menu(player_nr.begin());

menu function:

int menu(vector<int>::iterator x)
{
...
int a = x->size(); //<---- complains here
...
}

As far as I understood it Vectors have the arrow-operator defined in C++. Then why the fuck does the compilator complain of it?

Name: nambla_dot_org_rules_you 2011-04-20 14:54

Is it really that fucking difficult for you to post a complete piece of code that illustrates your problem?

Name: Anonymous 2011-04-20 14:58

>>2

void main()
{
vector<int> x;
x[0]=5;
vector<int>::iterator y = x.begin();
int z = y->size();
}

This is pretty much what I did. Any reasons this wouldn't compile? The thing I can't get to work is the arrow operator.

Name: Anonymous 2011-04-20 15:22

bump

Name: Anonymous 2011-04-20 15:36

>>4
Have you tried reading the error mess... wait, it's C++.

First of all, when you want to access properties of the iterator itself, you use '.' (dot). '->' or, equivalently, '(*it).' accesses methods/fields of the object that the iterator currently points at.

Second, where did you get the funny idea that iterators can have size? They are only required to define ++, --, +=(size_t), -=(size_t), and dereference (prefix '*' and '->' that uses it).

Name: Anonymous 2011-04-20 15:43

>>5

Which is why I use the arrow-operator (->) to use the "size()" function on the vector instead of on the iterator. Did I missunderstand how this works?

Name: Anonymous 2011-04-20 16:20

>>6
There is a small mistake here, you are dereferencing the iterator with the single arrow, (->), which will just get you the current object, i.e. in this case an int, which does not have a size method.
To access the originating vector, you should use the double arrow, (==>). Note the shaft length of two; if you only use one you'll access properties of the player object after casting it to a BigInteger, and if you use three you can call methods on the main function.

Name: Anonymous 2011-04-20 16:21

>>6

You did.

x is a vector<int>::iterator, ie an iterator over elements of the vector. So, when you use -> on the iterator you dereference it and get an int. The int obviously has no size() method.

You need to call size() on the vector, not the elements.

Name: Anonymous 2011-04-20 16:33

>>7
I laughed

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List