每当我尝试通过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;
}
基本上每当我尝试循环遍历向量时,它都会使我的应用程序崩溃,我检查了对象并且它是一个对象类,它不是空的。
还有什么导致此错误?
答案 0 :(得分:0)
for (auto cl = m_containers.begin(); cl != m_containers.end(); ++cl){
if (cl->second == container)
return static_cast<int32_t>(cl->first);
}
应解决您的问题
EDIT删除了&amp;如下所示
最小的例子
#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;
}
使用vs2013编译并按预期运行