2011-07-27 54 views
0

所以我有兩個方法下面發佈的問題。除了可怕的變量命名之外,我只有一個問題。不知怎的,在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

什麼是矢量Victor?對不起,無法抗拒。 –

+1

你應該修復你的縮進。 –

回答

2

getData將載體 「遮蔽」(隱藏)在你的類的載體中,然後當getData退出,矢量的所有內容都被釋放。你可能不應該在方法中重新聲明它。

+0

而且他們甚至沒有相同的類型! –