views:

1425

answers:

3

I am getting the following error while migrating VC6 code to VS2008. This code works fine in VC6 but gives a compilation error in VC9. I know it is because of a compiler breaking change. What is the problem and how do I fix it?

error C2440: 'initializing' : cannot convert
    from 'std::_Vector_iterator<_Ty,_Alloc>'
      to 'STRUCT_MUX_NOTIFICATION *'

Code

MUX_NOTIFICATION_VECTOR::iterator MuxNotfnIterator;

for(
    MuxNotfnIterator = m_MuxNotfnCache.m_MuxNotificationVector.begin();
    MuxNotfnIterator != m_MuxNotfnCache.m_MuxNotificationVector.end();
    MuxNotfnIterator ++ 
   )
{
    STRUCT_MUX_NOTIFICATION *pstMuxNotfn = MuxNotfnIterator; //Error 2440
}
+1  A: 

You'll need to dereference the iterator to get the appropriate struct (not sure why it worked before?):

STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;
arul
If my hypothesis is correct, what you suggest won't work.
Benoît
+3  A: 

I think this should do the trick:

   STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);
The Dark
+1 as this works. But I still need an answer on why it worked before and why not now. The specifics actually.I also read that there are breaking changes in STL with respect to iterators from VC7 onwards.
Bobby Alexander
There are "breaking" changes if your code was incorrect before - like assuming vector iterators are pointers. VC6 didn't exactly match the C++98 standard (if only because it's a few months older). VC7 adheres more closely to the standard, and furthermore adds extra debugging options. Smarter iterators help. Those still meet the standard requirements, but are more robust to abuse.
MSalters
+5  A: 

If it worked before, I am guessing MUX_NOTIFICATION_VECTOR is a typedef

typedef std::vector<STRUCT_MUX_NOTIFICATION> MUX_NOTIFICATION_VECTOR;

The iterator for a container can often be mistaken with a pointer (because it works the same way) and, in the case of some stl implementations, it can actually be a pointer (it probably was the case with STL provided with VC6). But there is no guarantee about that.

What you should do is the following :

STRUCT_MUX_NOTIFICATION& reference = *MuxNotfnIterator;
// or
STRUCT_MUX_NOTIFICATION* pointer = &(*MuxNotfnIterator);
Benoît
Yes it is a Typedef... and your declaration is perfect.Your reasoning seems to be correct. +1. Marking as the right answer unless someone has a more detailed explanation.Thanks Benoit.
Bobby Alexander