C++ 卡住矢量迭代器不兼容,找不到原因
C++ Stuck with vector iterators incompatible, can't find why
每当我尝试通过std::vector进行交互时,它会一直告诉我: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;
}
基本上每当我尝试通过向量循环时,它会一直崩溃我的应用程序,我检查了对象,它是一个对象类,它不是空的。
还有什么原因导致这个错误?
using
for (auto cl = m_containers.begin(); cl != m_containers.end(); ++cl){
if (cl->second == container)
return static_cast<int32_t>(cl->first);
}
应该能解决你的问题
删除&如下所示
一个最小的例子
#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
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义仅具有标头的类 - 找不到不存在的 cpp
- 适用于Android的Qt找不到任何兼容的设备答案找到但不知道如何
- C++ 卡住矢量迭代器不兼容,找不到原因