微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 c++11 时的未知运行时如果 c++14

如何解决使用 c++11 时的未知运行时如果 c++14

我在编译代码时遇到了以下错误。我试图查看我的代码以找到转换错误,但我没有看到。

编译时出现错误 compiles and running on g++ (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5)

当我在 Ubuntu 上编译时,我没有遇到任何错误并且可以运行。 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

这是错误

/builddir/build/BUILD/gcc-8.3.1-20191121/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_CharT,_Traits,_Alloc>::reference std::__cxx11::basic_string<_CharT,_Alloc>::operator[](std::__cxx11::basic_string<_CharT,_Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT,_Alloc>::reference = char&; std::__cxx11::basic_string<_CharT,_Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' Failed.
Aborted (core dumped)

main.cpp


//Here is where I can take a string and generate a pseudo-random password from it 
std::string passGen(std::string user)
{
    char passwordArray[9];
    int achar;
    srand (time(0));    
    for (int x=0; x <= 8; x++)
    {
        achar = (user[x] * (rand()%100))%26 + 97;
        passwordArray[x] = (char)achar;
    }   
    return passwordArray;
}
        
//This reads the filename that is passed in and builds the linked list of names and uses the password generator and generates passwords for each username. 
void readFile(std::string newFile){
    ifstream inFile(newFile);
    std::string firstWord;
    
    while(inFile >> firstWord)
    {
        listofNames->InsertAtHead(firstWord,passGen(firstWord));
        inFile.ignore(numeric_limits<streamsize>::max(),'\n');
    }
    inFile.close();
}

解决方法

我想出了问题所在。它在这里:

{
    char passwordArray[9];
    int achar;
    srand (time(0));    
    for (int x=0; x <= 8; x++)
    {
        achar = (user[x] * (rand()%100))%26 + 97;
        passwordArray[x] = (char)achar;
    }   
    return passwordArray;
}

当我将一个少于 9 个字符的名称传递给此方法时发生错误。如果名称太短,则 user[x] 将无法访问。我不得不将其更改为

user[x % user.size()]

我没有意识到编译器之间存在如此大的差异。看到我的错误后,我很惊讶它在 Ubuntu 中运行成功。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。