简体   繁体   中英

C++ STL: list with Pointers - Iterator cannot access?

I am struggeling with an STL list that holds Pointers of my "Object" object.

I declared:

list<Object*> objectlist;

and inserted via:

this->objectlist.push_back(new Object(address,value,profit));

and tried to iterate like in maps and others:

list<Object*>::iterator iter;

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    iter->print();
}

Where print() is a public Method of class Object;

Whats wrong here?

I cannot access via iterator to the objects in the list ?

You need (*iter)->print();

Since you have an iterator to a pointer, you have to first de-reference the iterator (which gets you the Object* ) then the arrow de-references the Object * and allows the call to print.

You are not incrementing your iterator! Change your while loop to a for loop like so:

for (list<Object*>::const_iterator iter = this->objectlist.begin(),
     end = this->objectlist.end();
     iter != end;
     ++iter)
{
    (*iter)->print();
}

(Also iter dereferences to a pointer, like the other answers have pointed out.)

You can access the value pointed by iterator with *iter

Also, remember to increment the iterator in each iteration. Otherwise you get stuck in an endless loop.

Like this:

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    (*iter)->print();
    iter++;
}

You could try the C++11 way (if possible). The dereference of the iterator comes free :)

for (auto& iter: this->objectlist)
{
     iter->print();
}

Also if print() is a const function you should use

for (const auto& iter: this->objectlist)

Instead of the for -loop you should use STL's for_each() - see the advantages of for_each over a simple for -loop.

Syntax

#include <algorithm> // 'for_each()' is in there

std::for_each(InputIterator first, // Iterator start position
              InputIterator last,  // Iterator end position
              Function fn);        // Unary function - executed on all elements in range

for_each example

Here's how your example looks std::for_each() instead of for( ... ) :

list<Object*> objectlist;

// insert some elements …

// Deletes all elements of 'objectlist'
std::for_each(objectlist.begin(), objectlist.end(), DeleteObj<Object*>());

Unary function

The unary function is implemented using a template, so you can use it with any type. Just make sure T is a pointer type !

template<class T> class DeleteObj
{
public:
    bool operator()(T obj) const
    {
        delete obj;
        return true;
    }
};

Btw. you don't have to implement this as template. And: you can bind a member function instead of such a implementation too!

Documentation

你应该使用以下方式:

(*iter)->print()

An iterator references an element in the list, so for a list of type T , dereferencing an iterator yields a value of type T .

In your case, T is actually a pointer - Object* . Hence, the iter-> call (which dereferences the iterator) yields a pointer. You have to dereference that pointer to get to the actuala object.

Try using

(*iter)->print()

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM