Hi guys,

I tried rewriting my C-ish C++ function to true C++, but I'm failing so far:

The first function (working)

const visualPart * entity::getVisualPart(unsigned int wantLOD){
    for(unsigned int i = 0; i < visualEntity.size(); i++){
        if(visualEntity[i]->LOD == wantLOD){
            return visualEntity[i];
        }
    }

    return NULL;
}

And the second (not working)

const visualPart * entity::getVisualPart(unsigned int wantLOD){
    for(unsigned int i = 0; i < visualEntity.size(); i++){
        if(*(visualEntity.begin() + i)->LOD == wantLOD){
            return *(visualEntity.begin() + i);
        }
    }

    return NULL;
}

The error I get from GCC's G++:

(...)entity.cpp|50|error: request for member `LOD' in `*(&(&(((std::vector<visualPart*, std::allocator<visualPart*> >*)((entity*)this)) + 44u)->std::vector<_Tp, _Alloc>::begin [with _Tp = visualPart*, _Alloc = std::allocator<visualPart*>]())->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ [with _Iterator = visualPart**, _Container = std::vector<visualPart*, std::allocator<visualPart*> >](((const ptrdiff_t&)((const ptrdiff_t*)(&((ptrdiff_t)i))))))->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-|

Could you please point out what I'm doing wrong?

TIA,
Nick

PS:

I've learned that iterators are pointers to the elements, so in this case, the iterators are of type visualPart**, therefore I dereference them once to get access to LOD, but this doesn't seem to work. Why not?

I dereference them once to get access to LOD, but this doesn't seem to work. Why not?

Dereferencing was actually missing ... i.e.

if( (*(visualEntity.begin() + i))->LOD == wantLOD){

I forgot these:

Snippet from entity.h

class entity {
    vector<visualPart*> visualEntity;
};

Snippet from visualPart.h

class visualPart {
    friend class entity;

private:
    unsigned int LOD;

};

And for mitrmkar: I missed it, but I added it and updated the error message as well. It still doesn't work. EDIT:
But I missed the parenthesis. Didn't know they were necessary, thanks a lot!

This question has already been answered. Start a new discussion instead.