简体   繁体   中英

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

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
}

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);

我认为这应该可以解决问题:

   STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);

您需要取消对迭代器的引用,以获取适当的结构(不确定为什么以前可以工作?):

STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;

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