1

错误

    QuBiEngine::QuBiEngine(ifstream& dnaFile)
{
    int i = 0;
    while(!dnaFile.eof()) //while the file isn't at its end
    {
        dna.push_back(""); //creates an element
        if(!dnaFile.good())//checks for failbits and other errors
        {
            dna[i] = "Not a valid sequence";
            i++;
            continue; 
        }
        getline(dnaFile, dna[i]);
        //checks to see if the character is valid ie: a, t, c, g
        for(int j=0; j<dna[i].length(); j++)
        {
            dna[i][j] = putchar(tolower(dna[i][j]));
            if((dna[i][j]!='a')||(dna[i][j]!='t')||(dna[i][j]!='c')||(dna[i][j]!='g'))
            {
                dna[i] = "Not a valid sequence";
                i++;
                break;
            }            
        }
        i++;        
    }
}

如果通过测试,这将获取 dnaFile 中的每一行ifstream并将其放入向量中,如果未通过,则将无效的内容放入向量中。

4

2 回答 2

1

我想通了,i++第二个 if 语句使它增加了两次,从而溢出了我的向量。

于 2013-05-08T00:45:16.767 回答
0

看来你可能break;在标记的地方失踪了:

    QuBiEngine::QuBiEngine(ifstream& dnaFile)
{
    int i = 0;
    while(!dnaFile.eof()) //while the file isn't at its end
    {
        dna.push_back(""); //creates an element
        if(!dnaFile.good())//checks for failbits and other errors
        {
            dna[i] = "Not a valid sequence";
            i++;
            continue; 
        }
        getline(dnaFile, dna[i]);
        bool bad = false;
        //checks to see if the character is valid ie: a, t, c, g
        for(int j=0; j<dna[i].length(); j++)
        {
            dna[i][j] = putchar(tolower(dna[i][j]));
            if((dna[i][j]!='a')||(dna[i][j]!='t')||(dna[i][j]!='c')||(dna[i][j]!='g'))
            {
                dna[i] = "Not a valid sequence";
                break;
            }            
        }
        i++;   
    }
}

除此之外:什么是dna变量?你能告诉我们它的任何声明和初始化吗?

除此之外,如果你dna的声明是为了你不会越界(不检查)

于 2013-05-08T00:37:23.667 回答