C++ C++;与向量迭代器不兼容,can';我不知道为什么

C++ C++;与向量迭代器不兼容,can';我不知道为什么,c++,vector,C++,Vector,每当我尝试通过std::vector进行交互时,它都会不断告诉我:向量迭代器不兼容 这就是让我崩溃的函数: // These 2 typedefs are declared in structs.h typedef std::pair<uint32_t, Object*> PlayerContainerPair; typedef std::vector<PlayerContainerPair> PlayerContainers; // This is a variabl

每当我尝试通过std::vector进行交互时,它都会不断告诉我:向量迭代器不兼容

这就是让我崩溃的函数:

// These 2 typedefs are declared in structs.h
typedef std::pair<uint32_t, Object*> PlayerContainerPair;
typedef std::vector<PlayerContainerPair> PlayerContainers;

// This is a variable from Player class in player.h
PlayerContainers m_containers;

// Definition of the function found in player.cpp
int32_t Player::GetContainerId(Object* container)
{
    for (PlayerContainers::const_iterator cl = m_containers.begin(); cl != m_containers.end(); ++cl){
        if (cl->second == container)
            return static_cast<int32_t>(cl->first);
    }

    return -1;
}
//这两个typedef在structs.h中声明
typedef std::pair PlayerContainerPair;
typedef std::向量播放器容器;
//这是Player.h中Player类的变量
玩家容器m_容器;
//在player.cpp中找到的函数的定义
int32_t Player::GetContainerId(对象*容器)
{
对于(PlayerContainers::const_iterator cl=m_containers.begin();cl!=m_containers.end();++cl){
如果(cl->秒==容器)
返回静态施法(cl->first);
}
返回-1;
}
基本上,每当我尝试循环遍历向量时,它就会不断地破坏我的应用程序,我已经检查了对象,它是一个对象类,它不是空的

还有什么原因导致此错误?

使用

for (auto cl = m_containers.begin(); cl != m_containers.end(); ++cl){
    if (cl->second == container)
        return static_cast<int32_t>(cl->first);
}
for(auto cl=m_containers.begin();cl!=m_containers.end();++cl){
如果(cl->秒==容器)
返回静态施法(cl->first);
}
应该解决你的问题

编辑删除了以下建议的(&A)

最简单的例子

#include <iostream>
#include <string>
#include <tuple>
#include <cstdint>
#include <vector>

struct Object {};

// These 2 typedefs are declared in structs.h
typedef std::pair<uint32_t, Object*> PlayerContainerPair;
typedef std::vector<PlayerContainerPair> PlayerContainers;

// This is a variable from Player class in player.h
PlayerContainers m_containers;

// Definition of the function found in player.cpp
int32_t GetContainerId(Object* container)
{

    for (auto cl = m_containers.begin(); cl != m_containers.end(); ++cl){
        if (cl->second == container)
            return static_cast<int32_t>(cl->first);
    }

    return -1;
}

int main()
{
    Object* o = new Object;
    m_containers.push_back(PlayerContainerPair(1, o));
    std::cout << GetContainerId(o);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
结构对象{};
//这两个typedef在structs.h中声明
typedef std::pair PlayerContainerPair;
typedef std::向量播放器容器;
//这是Player.h中Player类的变量
玩家容器m_容器;
//在player.cpp中找到的函数的定义
int32_t GetContainerId(对象*容器)
{
对于(自动cl=m_容器。开始();cl!=m_容器。结束();++cl){
如果(cl->秒==容器)
返回静态施法(cl->first);
}
返回-1;
}
int main()
{
对象*o=新对象;
m_容器。推回(PlayerContainerPair(1,o));

std::cout
c1
const\u迭代器
,但是
m\u容器。end()
是普通的
迭代器
。使用
cbegin()
cend()
,或者将
c1
声明为
迭代器
。或者它没有发生。@igortandenik:标准迭代器可以转换为(但不能从)并将其与const_迭代器进行比较。此外,这是一个运行时错误。这通常是因为您正在比较来自两个不同容器的迭代器。我经常在人们按值从函数返回容器时看到这种情况。例如,类似这样的情况:顺便说一句,它在发布模式下不会崩溃的原因是在发布模式下,检查迭代器被禁用。错误仍然存在,在代码中的某个地方,但你只是运气不好,它没有崩溃。@BaummitAugen它确实编译。它没有解决问题,请注意,我正在用VS2013调试。为什么你认为这会解决问题?我猜不兼容的常量迭代器就是问题所在,gia夫人sec@BaummitAugen或者再添加一个,使其成为
自动&&
,但这在这里是没有意义的。这里使用一个最小的示例ref:compiles刚刚好,但是,当整个项目处于调试模式时,每当调用该函数时,应用程序都会崩溃,但如果它处于发布模式,则不会发生任何事情,并且工作正常。