Vectors and Iterators

This is a discussion on Vectors and Iterators within the C++ Programming forums, part of the General Programming Boards category; I need some help figuring out some existing code. I've been given a project to recompile quite a bit of ...

  1. #1
    Registered User
    Join Date
    Jun 2008

    Vectors and Iterators

    I need some help figuring out some existing code. I've been given a project to recompile quite a bit of code in Visual Studio 2005 from 2003 and I'm having a bit of trouble with vectors and iterators. I'm a C/C++ novice, so humor me please. Here is a bit of the code:

    This is defined in EventBufferData.h:

    Type m_type;
    TimeStamp m_timeStamp;
    std::vector< Entry >::iterator mp_entry;
    std::string m_value;
    std::string m_value0;
    bool m_triggered0;
    TimeStamp m_timeStamp0;

    and this is a part of EventBufferData.cpp:

    EventBufferData::EventBufferData () :
    m_timeStamp.sysTime = "";
    m_timeStamp.simTime = "";
    m_timeStamp0.sysTime = "";
    m_timeStamp0.simTime = "";

    The problem comes from the line mp_entry(NULL). It seems this was fine in .NET 2003, but in 2005 I get an error : error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterat or(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'int' to 'const std::_Vector_iterator<_Ty,_Alloc>

    But what I really want to know is what is this line doing? mp_entry is defined as std::vector< Entry >::iterator mp_entry; and I can't find any examples online of an iterator being referenced as iterator_name(NULL).

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Instead of mp_entry(NULL), write: mp_entry()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    In fact, do the same for m_value and m_value0 (and even m_triggered0 if you wanted). An empty vector and empty string are the default, so if you use the default constructor or leave them out of the initializer list entirely they will be properly initialized. The default for bool is false, so if you used m_triggered0() it would be correctly initialized. However, unlike vector and strinng, since bool is a built-in type if you don't put it in the initializer list it won't be initialized at all and will contain garbage data.

    Edit: I thought mp_entry was a vector, so ignore my references to vector above.
    Last edited by Daved; 06-30-2008 at 10:33 AM.

  4. #4
    The larch
    Join Date
    May 2006
    It is strange though that this code would store an iterator for a longer time. Iterators are typically meant for ... iterating (locally).

    Watch out because vector's iterators tend to get invalidated quite often. (Storing indices might be somewhat safer.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors iterator
    By manzoor in forum C++ Programming
    Replies: 6
    Last Post: 05-11-2008, 04:02 PM
  2. C++ STL: Why use member iterators
    By Vandrian in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2008, 11:52 AM
  3. vectors and iterators?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2007, 11:39 AM
  4. Vectors, iterators and loops
    By Heavens in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2007, 06:05 AM
  5. vectors with same iterators
    By strickey in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2005, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21