Why do i have to assign this iterator pointer to a variable before i can use it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Liam Kurmos
    New Member
    • Feb 2011
    • 8

    Why do i have to assign this iterator pointer to a variable before i can use it?

    im using g++ on ubuntu 10.10

    i have a pointer to a vector of pointers to objects and i want to call a method on each of those objects.
    oc->features is defined as:

    vector<Feature* >* features;

    i have to do:

    Code:
    vector<Feature*>::iterator it;
        for (it=oc->features->begin();it!=oc->features->end();it++){
            
            Feature* a=*(it);
            int score=a->scoreFeature(view);
    }
    this works, but if i try to use the itterator directly, as in:

    Code:
            int score=*(it)->scoreFeature(view);
    I get the following compile error:

    ViewEvaluator.c pp:48: error: request for member ‘scoreFeat ure’ in ‘* it.__gnu_cxx::_ _normal_iterato r<_Iterator, _Container>::op erator-> [with _Iterator = Feature**, _Container = std::vector<Fea ture*, std::allocator< Feature*> >]()’, which is of non-class type ‘Feature*’
    //make[2]: *** [CMakeFiles/testOVAS.dir/ViewEvaluator.c pp.o] Error 1
    //make[1]: *** [CMakeFiles/testOVAS.dir/all] Error 2

    non-class type Feature* ?? i dont get it...

    can anyone shed light on what might be happening here?

    incidentally i thought it could be something to do with having a pointer to a container and using an itterator on it (at first i tried dereferencing that didnt work). I then realised that i have another example in my code using a pointer to vector and the analogous code works.
    Last edited by Niheel; Feb 9 '11, 05:08 PM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Try this

    Code:
    int score = (*it)->scoreFeature(view);
    to override the default operator precedence so that it is dereferenced with * before being deferenced with ->

    Comment

    • Liam Kurmos
      New Member
      • Feb 2011
      • 8

      #3
      :D thanks Banfa!

      it was one of those bugs where i just could see the obvious!

      Comment

      Working...