构造函数错误,在崩溃之前打印出'a'然后打印三角形

时间:2013-05-08 00:24:59

标签: c++ class vector constructor member

The Error

    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中的每一行,如果它通过了测试,则将其放入向量中,如果没有,则只将无效的东西放入向量中。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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被宣布为不会超出界限(没有检查)