• 13805阅读
  • 4回复

[提问]tostdstring 与tolocal8bit求教 [复制链接]

上一主题 下一主题
离线wtt611
 

只看楼主 倒序阅读 楼主  发表于: 2014-03-18
写程序遇到一个很奇怪的地方:
源代码如下:
class CMainRoutine
{
private:
QString m_qsInputNumber ;
QString m_qsInputExpression ;
public:

void CMainRoutine::SetInputData(string strNumber/*=""*/, string strExpr/*=""*/ )
{
    m_qsInputNumber = strNumber.c_str();
    m_qsInputExpression = strExpr.c_str();
}

void CMainRoutine::GetInputData(string &strNumber, string &strExpr)
{
    strExpr = m_qsInputExpression.toStdString();  //造成崩溃的是这里,把toStdString改为toLocal8Bit(),程序就没问题
    strNumber = m_qsInputNumber.toStdString();

}
}
int main()
{
CMainRoutine mainRoutine;
string strInputNum;
string strExpr;

mainRoutine.SetInputData("-0.369045012369525",“”);  //问题是这个字符串
mainRoutine.GetInputData(strInputNum, strExpr);  //程序到这里崩溃,提示是跟堆溢出有关的
}
请以上代码请问,为什么tostdstring会奔溃,而tolocal8bit就好着?

求解答!不吝赐教FRWF
Qt菜鸟,求指教!
离线dbzhang800

只看该作者 1楼 发表于: 2014-03-18
1. 这是你真实测试用的源码么?
2. 你的编译环境是什么,系统、编译器、Qt版本?
离线wtt611

只看该作者 2楼 发表于: 2014-03-18
回 dbzhang800 的帖子
dbzhang800:
1. 这是你真实测试用的源码么?
2. 你的编译环境是什么,系统、编译器、Qt版本?

windows,vs2005,qt4.6.3,然后其实是做Gtest测试项目,那个是被测代码,
Gtest测试部分的源码是这样的
TEST(AcceptDigitTest, HandleWithinLimit)
{
        CMainRoutine mainRoutine;
        string strInputNum;
        string strExpr;
        string strDisplayNum;

//测试已输入为大于-1的位数已达上限负浮点数
        mainRoutine.SetDisplayNum("-0.369856245632574");
        mainRoutine.SetInputData("-0.369856245632574");
        mainRoutine.AcceptDigit('2');
        mainRoutine.GetInputData(strInputNum, strExpr);    //debug跟踪至这里进去以后赋值语句出问题了
        strDisplayNum = mainRoutine.GetDisplayNum();
        EXPECT_STREQ("-0.369856245632574", strInputNum.c_str());
        EXPECT_STREQ("-0.369856245632574", strDisplayNum.c_str());
}
运行报错信息如下

Qt菜鸟,求指教!
离线realfan

只看该作者 3楼 发表于: 2014-03-18
在Win7 Qt5.2.1 MingW48下没有问题
#include <QCoreApplication>
#include <QDebug>
class CMainRoutine
{
private:
    QString m_qsInputNumber ;
    QString m_qsInputExpression ;
public:

    void SetInputData(std::string strNumber/*=""*/, std::string strExpr/*=""*/ )
    {
        m_qsInputNumber = strNumber.c_str();
        m_qsInputExpression = strExpr.c_str();
    }

    void GetInputData(std::string &strNumber, std::string &strExpr)
    {
        strExpr = m_qsInputExpression.toStdString();  //造成崩溃的是这里,把toStdString改为toLocal8Bit(),程序就没问题了
        strNumber = m_qsInputNumber.toStdString();
        //qDebug() << strExpr.c_str() << strNumber.c_str();
    }
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    CMainRoutine mainRoutine;
    std::string strInputNum;
    std::string strExpr;

    mainRoutine.SetInputData("-0.369045012369525","");  //问题是这个字符串
    mainRoutine.GetInputData(strInputNum, strExpr);  //程序到这里崩溃,提示是跟堆溢出有关的
    return a.exec();
}
离线wtt611

只看该作者 4楼 发表于: 2014-03-19
回 realfan 的帖子
realfan:在Win7 Qt5.2.1 MingW48下没有问题
#include <QCoreApplication>
#include <QDebug>
class CMainRoutine
{
....... (2014-03-18 21:28) 

谢谢你的测试!

话说......现在到底是神马情况.囧
Qt菜鸟,求指教!
快速回复
限100 字节
 
上一个 下一个