C++ 运行时错误 : operator< receiving rhs with ? ??值(value)..当然是我错过的愚蠢错误

标签 c++ debugging insert std

我正在尝试 map::insert 我定义的关键数据类型 (miniVec2ui).. 它在比较期间中断(它在比较时有虚假数据).. 它使用标准的 less 仿函数(我尝试了我自己的,但同样的问题) - 它正在传递 rhs.x = ???和 rhs.y = ???基本上。代码如下

miniVec2ui v233 = miniVec2ui(x,y);

m_pIdMap->insert(std::pair<miniVec2ui,uint>(v233,(uint)tmpPF.id));// std::pair<const miniVec2ui,uint>(v233, (uint)tmpPF.id) );
//////in other files I defined miniVec2ui,etc. (uint = unsigned int), tmpPF.id unimportant here, uint x; uint y;



typedef std::map<miniVec2ui,uint>       eIdMap;
eIdMap  *m_pIdMap;
m_pIdMap =      new eIdMap;//new std::map<miniVec2ui, uint >;
struct miniVec2ui {
    uint x;
    uint y;
    miniVec2ui(uint inx, uint iny):
                x(inx)
                ,y(iny) { }

    bool operator==(const miniVec2ui& rhs) const {
        return ((x == rhs.x) && (y == rhs.y));
    }
    bool operator!=(const miniVec2ui& rhs) const {
        return ( (x != rhs.x) || (y != rhs.y) );
    }

    bool operator< (const miniVec2ui& rhs) const {
        return ((x < rhs.x) && (y < rhs.y));

   }
    bool operator> (const miniVec2ui& rhs) const {
        return ((x > rhs.x) && (y > rhs.y));
    }

};

特别是 - 在 _function_base.h 中使用 __y 是 {x=???,y=???}... 这是反汇编:

...stuff...
#endif

{
  bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
200439C0 55                   push        ebp  
200439C1 8B EC                mov         ebp,esp  
200439C3 81 EC CC 00 00 00    sub         esp,0CCh  
200439C9 53                   push        ebx  
200439CA 56                   push        esi  
200439CB 57                   push        edi  
200439CC 51                   push        ecx  
200439CD 8D BD 34 FF FF FF    lea         edi,[ebp-0CCh]  
200439D3 B9 33 00 00 00       mov         ecx,33h  
200439D8 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
200439DD F3 AB                rep stos    dword ptr es:[edi]  
200439DF 59                   pop         ecx  
200439E0 89 4D F8             mov         dword ptr [ebp-8],ecx  
200439E3 8B 45 0C             mov         eax,dword ptr [__y]  
200439E6 50                   push        eax  
200439E7 8B 4D 08             mov         ecx,dword ptr [__x]  
200439EA E8 67 84 F1 FF       call        miniVec2ui::operator< (1FF5BE56h)  
200439EF 5F                   pop         edi   <--------------------- HERE ---------- <<

最佳答案

我不知道它是否与您发现的错误有关,但您的小于比较应该实现 strict weak ordering ,它不会:

bool operator< (const miniVec2ui& rhs) const {
    return ((x < rhs.x) && (y < rhs.y));
}

按照这个逻辑,m1不小于m2 , 和 m2不小于m1 :

miniVec2ui m1(5,5);
miniVec2ui m2(6,5);
std::cout << std::boolalpha;
std::cout << m1 < m2 << "\n";
std::cout << m2 < m1 << "\n";

std::map使用此条件来确定两个元素是否相等。你可能需要类似的东西

bool operator< (const miniVec2ui& rhs) const {
    if (x == rhs.x) return y < rhs.y;
    return x < rhs.x;
}

你应该尝试实现 operator>operator< 方面和 operator!=operator== 方面.

关于C++ 运行时错误 : operator< receiving rhs with ? ??值(value)..当然是我错过的愚蠢错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12544746/

相关文章:

php - 我可以避免在 INSERT 查询中用 NULL 值覆盖列吗?

php - 数据库或文件记录保存 - php

python - 在pycharm中调试python代码

iphone - 如何在 iphone 本身上访问我的 iphone 的日志文件

c++ - 精简 for 循环.. (C++)

c++ - 模板文件在 CodeLite 中不能正常工作?

visual-studio-2010 - NUnit 2.5.7 需要在 VS2010 下显式调试附加

c++ - Qsharedpointer类

c++ - "if the context from which the specialization is referenced depends on a template parameter"是什么意思?

c++ - deque 和 list STL 容器有什么区别?