我的载体正在消失

时间:2011-07-27 15:11:04

标签: c++ vector

所以我在下面发布的两种方法中遇到了问题。除了可怕的变量命名之外,我只有一个问题。不知怎的,在getData和cutRamping之间,我的矢量TwoDee消失了。我使用gdb跟踪它,并且在getData的末尾它填充了值,但是当我尝试手动跟踪它或程序试图在cutRamping中使用它时,它会抛出与向量中的迭代器相关的段错误,并且我告诉gdb打印矢量,它打印0x0而不是有效的内存地址。这两种方法实际上是背靠背调用的,所以我不知道如何在没有调用任何方法的情况下丢失向量TwoDee中的数据。任何帮助将不胜感激!

标题文件:

class Deviation
{
public:
    Deviation();
    void graphDev(std::string infile, std::string outfile1, std::string outfile2, std::vector<double> plats);

private:
    void getData();
    void cutRamping();
    void calcDev();
    void makeGraph();

    std::string input, output1, output2;
    std::vector<double> current, dx, dy, gcurrent, gdx, gdy, gdevx, gdevy, tempcurrent, tempdx, tempdy, plateaus;
    std::vector< std::vector<double> > TwoDee, FileOut, FileOutTemp; // File Out is a 2-D array that holds the Strength, b3-6 and a3-6 that are to be written to the file in that order where the Temp just holds each plateau value
    std::vector<double> curRow;
};

cpp文件:

void Deviation::getData()
{
    float f;
    vector< vector<float> > TwoDee;
    vector <float> curRow;
    TwoDee.clear();
    curRow.clear();
    FILE * pFile;
    ifstream inFile(input.c_str());
    // Counts the number of lines in the file
    unsigned int lines = count(istreambuf_iterator<char>(inFile), istreambuf_iterator<char>(), '\n');
    inFile.close();
    pFile = fopen (input.c_str(),"r");
    // Pushes all of the data into a 2-D vector
    for (unsigned int j = 0; j<lines; j++)
    {
        for (unsigned int i = 0; i<37; i++)
        {
            fscanf (pFile, "%f", &f);
            curRow.push_back((double)f);
            // Just says that it pushed back a value
            //cout<<"just pushed back "<<f<<endl;
        }
        TwoDee.push_back(curRow);
        curRow.clear();
    }

    fclose (pFile);

    for (unsigned int i = 0; i<TwoDee[0].size(); i++)
    {
        current.push_back(TwoDee[1][i]);
        dx.push_back(TwoDee[4][i]);
        dy.push_back(TwoDee[5][i]);
    }
}

void Deviation::cutRamping()
{
    // Moved these out of the loop for now
    tempcurrent.clear();
    tempdx.clear();
    tempdy.clear();
    unsigned int i = 0;
    bool keepgoing = true;
    for (unsigned int j = 0; j<plateaus.size(); j++)
    {
        keepgoing = true;
        while (i<current.size()&&keepgoing)
        {
            if (fabs(plateaus[j]-current[i]) < 0.2 && i<current.size()) // (0.2 is the tolerance to determine if the current is ramping or not)
            {
                tempcurrent.push_back(current[i]);
                tempdx.push_back(dx[i]);
                tempdy.push_back(dy[i]);
                FileOutTemp[0].push_back(TwoDee[3][i]);

    // ...
    // Rest of the method that never gets called
}

1 个答案:

答案 0 :(得分:2)

getData中的向量是“阴影”(隐藏)类中的向量,然后当getData退出时,向量的所有内容都被释放。您可能不应该在方法中重新声明它。