我已经使用断点来检查在将元素插入 Multi-set 时是否调用了我的比较函数,但它从未到达断点。
错误Unhandled exception at 0x003c5a71 in Regular_Calibration_d.exe: 0xC0000005: Access violation reading location 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;
}
//多集声明
std::multiset<SMS, LessSMSTime> SMSSet;
// 在某个函数中
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();
}