2014-02-25 55 views
0

所以我有我的代碼如下。我正在鑲嵌矩陣/或pgm圖像。通過鑲板我的意思是在一定數量的列和行中重複自己。它看起來像是其中一個窗戶,窗玻璃全部將每個玻璃部分分開。向量向量的段錯誤

一個例子:http://1.bp.blogspot.com/_OLskT-GO5VE/TGqgrSX_o_I/AAAAAAAAA-4/vcCdn6hA3fI/s320/2007_12_warhol_cambell_soup.jpg這是湯罐的一個4×8矩陣(其本身是像素的一個巨大的矩陣

相信段故障是否與索引= K某處發生涉及,但我不能。發現任何錯誤,我調整大小的矩陣,這不應該是它

編輯:。固定例如

void panel(vector <VecofInts> &p, int inputRow, int inputColumn) 
{ 
    int i, j, v = 1, g = 1, k = 0, row, col; 

    row = p.size();//obtaining the original rows 
    col = p[0].size();//obtaining the original columns 

    p.resize((r * row)); //sets up the new matrix so I can add new elements. 

/* This is my first test loop for the columns; I know I can create a single loop 
for rows and columns but this will help me find problems more easily */ 

    while(v < c){ 
     ... 
    } 

/* this is the loop I'm having trouble with */ 

    v=1; 

    while(v < c){ 
     k = row; 
     while(g < r){ 
      for(i = 0; i < row; i++){ 
       k = k + i; 
       for(j = 0; j < col; j++){ 
        p[k].push_back(p[i][j]); 
       } 
      } 
      g++; 
      k++; //this allows the g<r loop to continue and k not repeat itself 
        //in the first i loop again. 
     } 
     v++; 
    } 
} 
+1

嘗試用標誌-g編譯它,並用如GDB調試器中運行,這給yoiu的代碼,該段錯誤發生的確切LNE。請參閱http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html – tgmath

+0

其中定義了變量「c」? –

+0

我必須爲調試器做一些工作。我會沿途更新 – TrickyNicky

回答

0

從你的例子是不是太清楚什麼是你的意圖。
順便說一句,我的建議是一般方法:

typedef std::vector<int> VecofInts; 
typedef std::vector<VecofInts> Matrix; 
typedef std::vector<int>::iterator RowIterator; 

void panel(const Matrix& m, int inputRow, int inputColumn) { 
    const int column = m.size(); /* Number of column, each colum is a vec of int */ 
    RowIterator it; 
    for(int i=0; i != column; i++) { 
     it = m[i].begin(); 
     /* m[i] represent a row on your matrix */ 
     for(; it != m[i].end; it++) { 
      ... 
     } 
    } 
} 
0

當你在for循環,你會得到K = 0使用k = k + i, 1,3,6,10,15 ...

我認爲你的目的是k++

編輯:

請參見注釋爲k = k + i

while(v < c){ 
    k = row; 
    while(g < r){ 
     for(i = 0; i < row; i++){ 
      k = k + i; // ####### k = (row), (row+1), (row+3), (row+6), (row+10),(row+15).... 
      for(j = 0; j < col; j++){ 
       p[k].push_back(p[i][j]); 
      } 
     } 
     g++; 
     k++; //this allows the g<r loop to continue and k not repeat itself 
       //in the first i loop again. 
    } 
+0

當For(i)循環退出時,不會退出k嗎?我有k =行;再次重新申請的基礎價值,所以它不會反覆無止境。 – TrickyNicky

+0

@TickyNicky'p [k]'代表什麼?實際上你有一系列的p [row + 0] .push,p [row + 1] .push,p [row + 3] .push,p [row + 6] .push,p [row + 10] .push在你的代碼中 –

+0

@TrickyNicky - 你知道k的最大值必須是什麼,但是你依靠錯誤的邏輯來退出循環。保持簡單 - 爲什麼不檢查以確保k在界限內,如果沒有,那麼你有問題(或退出循環)。 – PaulMcKenzie