c++ - 设置 QLineEdit 时的 std::out_of_range

标签 c++ qt-creator std stdstring outofrangeexception

在我的 C++ 应用程序中,我必须选择一个带有 QFileDialog 类的文件。 然后,我验证我的文件名是否正确(它必须以“VG”开头)。

我的文件有这样的结构:VGx-xx-xxxx-xxx-xxx.pigs

之后,我在关联的QLineEdit中设置了它。 但是每次我选择一个好的文件时,它就会崩溃,我不明白为什么。

这是我的不同功能:

打开 QFILE 对话框窗口

/** OPEN FILE DIALOG WINDOW  **/
void VGCCC::selectPIGSFile()
{
    QString pigsFile = QFileDialog::getOpenFileName
    (
        this,
        tr("Select PIGS file"),
        "./../VGCColorConfigurator/Inputs",
        tr("Document files (*.pigs *.igs)"),
        0,
        QFileDialog::DontUseNativeDialog
    );

    pigsPath = pigsFile;
    if(verifyPIGSFileValidity(pigsPath.toStdString()))
    {
        m_filePathLine->setText("");
        m_filePathLine->setText(pigsPath);
        m_testTextEdit->insertPlainText("File selected : "+pigsPath+"\n");
    }
    else
    {
        m_filePathLine->setText("Please select a valid PIGS (Format VGx-xx-xxxx-xxx-xxx.pigs)");
        m_testTextEdit->insertPlainText("Uncorrect PIGS file.\n");
    }
}

文件名验证

/** VERIFY SELECTED PIG FILE **/
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);
    std::string verification = pigsNameTable[0].erase(2,2);
    std::string headerName = "VG";

    if(!verification.compare(headerName))
    {
        m_testTextEdit->insertPlainText("PIGS name is correct");
        return true;
    }
    else
        return false;
}

拆分方法

/** SPLIT PIGS NAME INTO TABLE **/
std::string* VGCCC::splitPIGSName(std::string pigsPath)
{
    std::string pigsPathToSplit = pigsPath;
    std::string delimiter = "-";
    size_t position = 0;
    int i=0;
    std::string token;

    while ((position = pigsPathToSplit.find(delimiter)) != std::string::npos)
    {
        token = pigsPathToSplit.substr(0, position);
        std::cout << token << std::endl;
        pigsNameTable[i] = token;
        i++;
        pigsPathToSplit.erase(0, position + delimiter.length());
    }
    pigsNameTable[4] = pigsPathToSplit.c_str();
    std::cout << pigsPathToSplit << std::endl;
}

最佳答案

bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);
    std::string verification = pigsNameTable[0].erase(2,2);
    std::string headerName = "VG";

    if(!verification.compare(headerName))
    {
        m_testTextEdit->insertPlainText("PIGS name is correct");
        return true;
    }
    else
        return false;
}

不安全是因为:

1- 您不检查 pigsNameTable 是否在索引处有元素(如果是 vector?)或键(如果是 map ?)0

2- 您没有检查 pigsNameTable[0] 是否有超过 2 个元素。参见 erase documentation :

pos: Position of the first character to be erased. If this is greater than the string length, it throws out_of_range.

你可以简单地做:

bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);

    if ( /* test is pigsNameTable[0] exists depending on pigsNameTable's type */ )
    {
        return pigsNameTable[0].find( "VG" ) == 0; // return true if pigsNameTable[0] starts with "VG"
    }
    else
    {
        return false;
    }
}

如果 pigsNameTable 是一个 vector 测试可以是 !pigsNameTable.empty(),如果它是一个 map, pigsNameTable.find(0) != pigsNameTable.end()....

关于c++ - 设置 QLineEdit 时的 std::out_of_range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980199/

相关文章:

c++ - 如何在三元运算符中打破 for 循环

c++ - C++ 标准是否需要 `#include <math.h>` 来定义 `abs` 中的 `<cmath>` 重载?

qt4 - 使用环境变量值设置包含路径

c++ - 有没有办法将 std::array 与文字或初始值设定项列表一起用于比较,就像赋值一样?

c++ - 迭代器(std::map)在 C++ 中是如何工作的?

c++ - 单例日志类中运算符 << 的问题

c++ - 双击 QLabel

android - Qt Creator中Android项目编译报错 "cannot find -lc++"如何解决?

qt - Qt Creator 2.5.2 中缺少移动 Qt 应用程序

c++ - 通过编写包装器将 RogueWave 替换为标准库