Saturday, July 5th 2008, 4:29am UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, April 15th 2008, 9:22am

C++ STL, iterator and const_iterator

Hi,

I'm having troubles using iterator and const_iterator.

Let's consider this code :

Source code

1
2
3
4
5
LabeledGraph::node_children_t::iterator it;

    for (it=g->children().begin(); it!=g->children().end(); ++it) {
      // do someting with (*it)
    }


this code crashes when it is of type iterator, but works well when it is of type const_iterator. I don't understand why :(

More surprisingly, writing

Source code

1
printf("%s\n", (*(g->children().begin()))->toString().c_str());


works well, while writing :

Source code

1
2
3
LabeledGraph::node_children_t::iterator it = g->children().begin();

printf("%s\n", (*it)->toString().c_str());


crashes. It seems that dereferencing it causes crashes when it is of type iterator.

Any explanations ?

My problem is related to that specific code. I always use iterators and everything goes well. Perhaps it is the fact that the code runs in a thread ?

Thanks
  • Go to the top of the page

2

Sunday, April 20th 2008, 1:13am

RE: C++ STL, iterator and const_iterator

I not sure if I can visualize your code. Of what I see, it could be a scope issue.
I hope this is of help.
Fred
  • Go to the top of the page

Rate this thread