错误C2440:'初始化':无法从'std :: _ Vector_iterator '转换为'type *'

时间:2021-07-27 20:10:19

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?

我将VC6代码迁移到VS2008时收到以下错误。此代码在VC6中工作正常,但在VC9中给出了编译错误。我知道这是因为编译器破坏了变化。有什么问题,我该如何解决?

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
}

3 个解决方案

#1


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

如果之前有效,我猜MUX_NOTIFICATION_VECTOR是一个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.

容器的迭代器通常可以用指针错误(因为它以相同的方式工作),并且在某些stl实现的情况下,它实际上可以是指针(可能是随VC6提供的STL的情况)。但是不能保证这一点。

What you should do is the following :

你应该做的是以下几点:

STRUCT_MUX_NOTIFICATION& reference = *MuxNotfnIterator;
// or
STRUCT_MUX_NOTIFICATION* pointer = &(*MuxNotfnIterator);

#2


I think this should do the trick:

我认为这应该做的伎俩:

   STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);

#3


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

您需要取消引用迭代器以获取相应的结构(不确定它之前的原因是什么?):

STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;

#1


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

如果之前有效,我猜MUX_NOTIFICATION_VECTOR是一个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.

容器的迭代器通常可以用指针错误(因为它以相同的方式工作),并且在某些stl实现的情况下,它实际上可以是指针(可能是随VC6提供的STL的情况)。但是不能保证这一点。

What you should do is the following :

你应该做的是以下几点:

STRUCT_MUX_NOTIFICATION& reference = *MuxNotfnIterator;
// or
STRUCT_MUX_NOTIFICATION* pointer = &(*MuxNotfnIterator);

#2


I think this should do the trick:

我认为这应该做的伎俩:

   STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);

#3


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

您需要取消引用迭代器以获取相应的结构(不确定它之前的原因是什么?):

STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;