문제

I'm trying to convert a vector iterator to the object pointer in the function below:

std::vector<some_object> some_objects;

some_object* getsome(int index)
{
    if( index < ledges.size() && index >= 0)
    {
        return &(*some_objects[index]);
    }
    return NULL;
}

However the compiler throws an error : "no match for 'operator*." What is the proper way to handle this?

도움이 되었습니까?

해결책

The problem is that the vector is holding objects of type some_object but not some_object *. You are trying to dereference a non-pointer type and compiler is complaining about it. Try -

return  &(some_objects[index]);

or

return &(some_objects.at(index));

다른 팁

The [] operator of your some_objects vector has return type some_object& (a reference), so to get a pointer to it you just need to use the & operator directly on that:

return &some_objects[index];
some_object* getsome( std::vector<some_object>::size_type index)
{
   return ( index < some_objects.size() ? &some_objects[index] : NULL );
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top