C++ 在multiset中,代码不会进入比较函数并抛出错误

C++ 在multiset中,代码不会进入比较函数并抛出错误,c++,qt,stl,multiset,C++,Qt,Stl,Multiset,我使用断点来检查在将元素插入多个集合时是否调用了比较函数,但它从未到达断点 常规校准中0x003c5a71处的错误未处理异常\u d.exe:0xC0000005:访问冲突读取位置0x00000014。 我正在粘贴下面的代码。请让我知道我哪里做错了。 有几件重要的事情我有疑问 1) 在实际插入sms.message之前,我正在处理它 多集你们认为我做错了什么吗 究竟是什么造成了这个问题 2) 如果我想一想 字符串操作有问题,但为什么呢 不点击比较时间的comapre函数 下面是我的代码 短信

我使用断点来检查在将元素插入多个集合时是否调用了比较函数,但它从未到达断点

常规校准中0x003c5a71处的错误
未处理异常\u d.exe:0xC0000005:访问冲突读取位置0x00000014。

我正在粘贴下面的代码。请让我知道我哪里做错了。 有几件重要的事情我有疑问

  • 1) 在实际插入sms.message之前,我正在处理它 多集你们认为我做错了什么吗 究竟是什么造成了这个问题

  • 2) 如果我想一想 字符串操作有问题,但为什么呢 不点击比较时间的comapre函数

下面是我的代码

短信的结构

struct SMS
{
  SMS(const SMSType::Enum e, const QString& s);
  QDateTime          time;
  SMSType::Enum      smsType;
  QString            message;
};
//信息结构

SMS::SMS( const SMSType::Enum e, const QString& s )
: smsType( e ), message( s )
{
   time = QDateTime::currentDateTime();

}
//比较函数

bool SMS_list::LessSMSTime::operator ()( const SMS& left,
                     const SMS& right ) const
{

  QDate date_left  = left.time.date();
  QDate date_right = right.time.date();

  if( date_left.year() < date_right.year() )
    return true;
  else if( date_left.year() > date_right.year() )
    return false;

  if( date_left.month() < date_right.month() )
    return true;
  else if( date_left.month() > date_right.month() )
    return false;

  if( date_left.day() < date_right.day() )
    return true;
  else if( date_left.day() > date_right.day() )
    return false;

  QTime time_left  = left.time.time(); 
  QTime time_right = right.time.time(); 

  if( time_left.hour() < time_right.hour() )
         return true;
    else if( time_left .hour() > time_right.hour() )
         return false;

 if( time_left.minute() < time_right.minute() )
         return true;
    else if( time_left.minute() > time_right.minute() )
         return false;

  if( time_left.second() < time_right.second() )
          return true;
     else if( time_left.second() > time_right.second() )
          return false;

  if( time_left.msec() < time_right.msec () )
          return true;

  return false;
}
//字符串操作

void SMSInterface::output( const SMSType::Enum type, QString str_qt ) const
{

 // convert QString to std::String
 std::string str = str_qt.toStdString();
 QMutex mutex;
 mutex.lock();

 if( str[ str.length() - 1 ] == '\n' )
 {
  str = std::string( str.cbegin(),   str.cbegin() + str.length() - 1 );
 }

 //convert std::string to QString
 QString str_to_qt = QString::fromStdString ( str );
// QString str_to_qt = QString::fromUtf8 ( str.c_str() );

    SMS sms( type, str_to_qt );
    sms_list_->add_sms( message ); // inside this function multi-set insertion is called
bla bala 


 mutex.unlock();
}
如前所述

“访问冲突读取位置0x00000014”

建议您尝试调用成员函数,或读取某个为null的对象的属性

当你发布更多代码时,我们可以看到

短信列表->添加短信(短信)

作为唯一可见的可以是空的东西,事实上,它没有初始化,是空的,因此出现了问题。

如前所述

“访问冲突读取位置0x00000014”

建议您尝试调用成员函数,或读取某个为null的对象的属性

当你发布更多代码时,我们可以看到

短信列表->添加短信(短信)


因为我们看到的唯一东西是
null
,事实上,它没有初始化,是null,因此出现了问题。

哪一行导致了断言?(你说它从不调用的比较函数的位置有点奇怪)你认为错误的字符串操作在哪里?你试过在调试器中运行你的程序吗?它将帮助您找出崩溃发生的位置,并为您提供工具来尝试找出它发生的原因。当代码到达MessageSet.insert(sms)时,它将崩溃。@doctorlove,哪一行导致断言?代码中显示的最后一行。是的,我尝试在调试模式下运行,它指向xtree.cpp文件中的Nodeptr&_Root()const{//return Root of nonmutable tree return(this->_Parent(this->_Myhead));},哪一行导致断言?(你说它从不调用的比较函数的位置有点奇怪)你认为错误的字符串操作在哪里?你试过在调试器中运行你的程序吗?它将帮助您找出崩溃发生的位置,并为您提供工具来尝试找出它发生的原因。当代码到达MessageSet.insert(sms)时,它将崩溃。@doctorlove,哪一行导致断言?代码中显示的最后一行。是的,我尝试在调试模式下运行,它指向xtree.cpp文件中的Nodeptr&_Root()常量{//返回不可交换树返回的根(this->_Parent(this->_Myhead));}??
SMSSet.insert( sms ) ;
void SMSInterface::output( const SMSType::Enum type, QString str_qt ) const
{

 // convert QString to std::String
 std::string str = str_qt.toStdString();
 QMutex mutex;
 mutex.lock();

 if( str[ str.length() - 1 ] == '\n' )
 {
  str = std::string( str.cbegin(),   str.cbegin() + str.length() - 1 );
 }

 //convert std::string to QString
 QString str_to_qt = QString::fromStdString ( str );
// QString str_to_qt = QString::fromUtf8 ( str.c_str() );

    SMS sms( type, str_to_qt );
    sms_list_->add_sms( message ); // inside this function multi-set insertion is called
bla bala 


 mutex.unlock();
}