error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'type *'
-
13-09-2019 - |
Question
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
}
Solution
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);
OTHER TIPS
I think this should do the trick:
STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);
You'll need to dereference the iterator to get the appropriate struct (not sure why it worked before?):
STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;