c++ - 从 pair 到 Vector2f 的转换导致超出范围错误

标签 c++ vector

我正在使用 Kruskal 算法实现迷宫生成。除了最后一步,我已经完成了所有操作,即将我的浮点对 vector (vector>) 转换为 Vector2f vector (vector)。这是我用来执行此操作的函数:

void ConvertToVector2F(vector<std::pair<float, float>> verts, vector<Vector2f> verts2f)
{
    //convert verts vector of pairs to vector of vector2f (MS stuff)

    for (int i = 0; i < verts.size(); i++)
    {
        Vector2f newVert = { verts[i].first, verts[i].second };
        verts2f.push_back(newVert);
    }
}

这个是从main函数调用的,转换后从main函数返回Vector2f对象的vector。当我从 main 返回 verts2f 时,它会导致一系列错误:

"Unhandled exception at 0x76EFC42D in Lab1.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0025EDF8."

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

这些不会导致整个功能崩溃,但是迷宫生成不起作用。

当我传递 vector2fs 的通用 vector 时,例如 { {0,0} } ,不会发生错误。

我怀疑这是我处理 vector 对象的方式的问题,特别是因为我提到的错误都提到了一些关于

std::vector< float,std::allocator >* 对象。

我真的不知道该怎么做才能解决这个问题。我只知道是什么导致了这个问题。

任何人都可以阐明这个问题吗?我在 Windows 7 x64 机器上用 Visual Studio 2013 编写。

最佳答案

将函数声明为

void ConvertToVector2F(vector<std::pair<float, float>> verts, 
                       vector<Vector2f> &verts2f);

也就是将第二个参数声明为对 vector 的引用。

至于我,那么我会将函数声明为

void ConvertToVector2F( const vector<std::pair<float, float>> &verts, 
                       vector<Vector2f> &verts2f);

关于c++ - 从 pair 到 Vector2f 的转换导致超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963743/

相关文章:

c++ - 如何在 C/C++ 中临时禁用宏扩展?

c++ - 计时代码 "C2440: ' <function-style-cast >' : cannot convert from ' _CR' to 'std::chrono::milliseconds' 中的一个奇怪错误

c++ - 仅 header 类 + 未定义的函数引用,仅当返回该类的对象时

c++ - c2955 错误 - 使用类模板需要参数列表

c++ - 哈希函数和随机排列

c++ - "CoInitialize failed"当一个函数(来自库)包含在 VC++ MFC 项目中时

c++成员在父子构造函数之间更改值

c++ - vector::end() 是否保证大于 vector 中任何对象的存储地址?

c++ - 如何设置两个 vector<unique_ptr<...>> 彼此相等?

c++ - 创建自己的vector Class,错误很多