简体   繁体   English

解除引用迭代器会导致“无法转换”错误,因为它似乎不应该

[英]dereferencing iterator causes 'can not convert' error when it seems it shouldn't

Using VS 2008, the target environment is Windows CE with an ARM processor if that makes a difference. 使用VS 2008,目标环境是带有ARM处理器的Windows CE,如果有所不同的话。 I know that the compiler we are using is kind of dated as well... 我知道我们使用的编译器也有点过时了......

The basic problem I am having is that I am trying to make my own iterator for a map wrapper I wrote and overloading the operator->() is giving me trouble. 我遇到的基本问题是我正在尝试为我编写的地图包装器创建自己的迭代器并重载运算符 - >()给我带来麻烦。 This is the bit of code that is giving me issues: 这是给我一些问题的代码:

const pair<wstring, PropertyMap*>* ObjectMapIterator::operator->() const
{
    return &*m_MapIter;
}

I know that it doesn't really make sense to return a const variable usually, but I can't seem to figure out how to do this in a way that lets the rest of the program be const-correct without doing that. 我知道通常返回一个const变量并没有多大意义,但我似乎无法弄清楚如何以一种方式执行此操作,使程序的其余部分保持正确而不这样做。

The error I get is this: 我得到的错误是这样的:

error C2440: 'return' : cannot convert from 'const std::pair<_Ty1,_Ty2> *' to 'const std::pair<_Ty1,_Ty2> *' 错误C2440:'return':无法从'const std :: pair <_Ty1,_Ty2> *'转换为'const std :: pair <_Ty1,_Ty2> *'

The header for this iterator looks like this: 这个迭代器的标题如下所示:

class ObjectMapIterator
{
public:
    ObjectMapIterator(const ObjectMap& rObjectMap);
    const ObjectMapIterator& operator++(int rhs);
    std::pair<std::wstring, PropertyMap*> operator*() const;
    const std::pair<std::wstring, PropertyMap*>* operator->() const;
    bool isDone() const;

private:
    const std::map<std::wstring, PropertyMap*>* m_pPropertyMaps;
    std::map<std::wstring, PropertyMap*>::const_iterator m_MapIter;
};

As you can see, both the m_MapIter and the return value of the overloaded operator are the same... I took all of the const statements out of the .h and .cpp files for this part of the project and recompiled with the same error, so I don't think this is an issue with that. 正如您所看到的,m_MapIter和重载运算符的返回值都是相同的......我从.h和.cpp文件中取出了所有const语句,用于项目的这一部分并重新编译时出现相同的错误所以我不认为这是一个问题。

If I do something like this instead, the program will compile: 如果我做这样的事情,程序将编译:

const pair<wstring, PropertyMap*>* ObjectMapIterator::operator->() const
{
    const pair<wstring, PropertyMap*>* returnVal = new pair<wstring, PropertyMap*>(*m_MapIter);
    return returnVal;
}

I know that doing that would lead to a memory leak, I didn't put it into a smart pointer just to save space for posting this. 我知道这样做会导致内存泄漏,我没有把它放入智能指针只是为了节省空间来发布这个。

Here is the whole .cpp file in case you might find that relevant: 这是整个.cpp文件,以防您可能发现相关:

#include "stdafx.h"
#include "ObjectMap.h"

using namespace std;

ObjectMapIterator::ObjectMapIterator(const ObjectMap& rObjectMap)
    :m_pPropertyMaps(&(rObjectMap.m_PropertyMaps)),
     m_MapIter(m_pPropertyMaps->begin())
{}

const ObjectMapIterator& ObjectMapIterator::operator++(int rhs)
{
    if(m_MapIter != m_pPropertyMaps->end())
    {
        m_MapIter++;
        return *this;
    }
    else
    {
        return *this;
    }
}

pair<wstring, PropertyMap*> ObjectMapIterator::operator*() const
{
    return *m_MapIter;
}

const pair<wstring, PropertyMap*>* ObjectMapIterator::operator->() const
{
    return &*m_MapIter;
}

bool ObjectMapIterator::isDone() const
{
    return m_MapIter == m_pPropertyMaps->end();
}

The ObjectMapIterator definition is inside of the ObjectMap.h file. ObjectMapIterator定义位于ObjectMap.h文件中。 So I am not forgetting to include ObjectMapIterator. 所以我不会忘记包含ObjectMapIterator。

I've been scratching my head over this for too long. 我一直在摸不着头脑。 Please let me know if you figure anything out. 如果你搞清楚了,请告诉我。 Thanks! 谢谢!

std::map::const_iterator returns a temporary, not a reference, so you are trying to take address of that temporary and return it. std::map::const_iterator返回一个临时的,而不是引用,所以你试图获取那个临时的地址并返回它。

Why not simply return pair by value? 为什么就不能返回pair通过价值?

 std::pair<std::wstring, PropertyMap*>* ObjectMapIterator::operator->() const { return *m_MapIter; } 

Actually, if your operator will return std::map::const_iterator::pointer , as std::map::const_iterator::operator->() does, everything will be ok: 实际上,如果你的运算符将返回std::map::const_iterator::pointer ,就像std::map::const_iterator::operator->()那样,一切都会好的:

 std::map<std::wstring, PropertyMap*>::const_iterator::pointer operator->() const { return &*m_MapIter; } 

Also, as far as value, returned by std::map::const_iterator::operator->() is implementation defined, may be it would be better to use 另外,就std::map::const_iterator::operator->()返回的值是实现定义的,可能会更好用

 auto operator->() const -> decltype(m_MapIter.operator->()) { return (m_MapIter.operator->()); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM