2013-03-02 16 views
2

好的,我正在實現一個動態的2維矩陣類。對於一個基礎,這是我到目前爲止有:無法以任何方式讀取函數類

template <typename Type> 
class dyMatrix { 
    private: 
     Type *mat; 

     int lines, columns; 
     int length; 

     void assimilate (const dyMatrix<Type>& that) { 
      lines = that.lines; 
      columns = that.columns; 

      length = that.length; 
     } 

    public: 
     dyMatrix (int height, int width) 
      : lines(height), columns(width), mat(0) 
     { 
      length = lines * columns; 
      mat = new Type[length]; 
     }; 

     // --- 

     int getLines() { 
      return lines; 
     }; 

     int getColumns() { 
      return columns; 
     }; 

     int getLength() { 
      return length; 
     } 

     // --- 

     Type& operator() (int i, int j) { 
      return mat[j * columns + i]; 
     }; 

     Type& operator() (int i) { 
      return mat[i]; 
     }; 

     // --- 

     dyMatrix (const dyMatrix<Type>& that) { 
      this->assimilate(that); 

      // --- 

      mat = new Type[length]; 

      for (int i = 0; i < length; i++) { 
       mat[i] = that.mat[i]; 
      } 
     }; 

     dyMatrix& operator= (const dyMatrix<Type>& that) { 
      Type *local_mat = new Type[that.length]; 

      for (int i = 0; i < that.length; i++) { 
       local_mat[i] = that.mat[i]; 
      } 

      // --- 

      this->assimilate(that); 

      delete[] mat; 
      mat = local_mat; 

      // ---- 

      return *this; 
     }; 

     ~dyMatrix() { 
      delete mat; 
     }; 
}; 

我的問題是,試圖讀取輸入到它時,我一直有一些特定的問題。例如,我主要有如下():

int main() { 
    int lanes, cols; 
    cin >> lanes >> cols; 

    dyMatrix<int> map(lanes, cols); 

    /* CONTINUE READING */ 

    cout << endl; 
    for (int i = 0; i < lanes; i++) { 
     for (int j = 0; j < cols; j++) { 
      cout << map(i, j) << ' '; 
     } 
     cout << endl; 
    } 
} 

當它是一個註釋部分,如果我把下面的代碼行:

int aux; 
for (int i = 0; i < lanes; i++) { 
    for (int j = 0; j < cols; j++) { 
     cin >> map(i, j); 
    } 
} 

或:

int aux; 
for (int i = 0; i < lanes; i++) { 
    for (int j = 0; j < cols; j++) { 
     cin >> aux; 
     map(i, j) = aux; 
    } 
} 

或者甚至:

int aux; 
for (int i = 0; i < lanes; i++) { 
    for (int j = 0; j < cols; j++) { 
     cin >> aux; 
     map(i, j) = i + j; // !!! 
    } 
} 

...它不起作用。但是,出於某種原因,以下內容會:

int aux; 
for (int i = 0; i < lanes; i++) { 
    for (int j = 0; j < cols; j++) { 
     // cin >> aux; 
     map(i, j) = i + j; // Still the same thing as before, logically 
    } 
} 

我不明白。這裏發生了什麼?


編輯:對不起,忘了包括我的調試過程。

輸入:

3 5 
1 2 3 4 5 
1 2 3 4 5 

第一線具有矩陣的尺寸。第二行和第三行是寫入它的值;但是,在第三行末尾按Enter之後,程序崩潰。

,它甚至沒有與該輸入工作:

3 5 
1 
2 
3 
4 
5 
1 
2 
3 
4 
5 

其第二個5之後崩潰,再次。

+0

你是什麼意思的「......它不會工作」? – 2013-03-02 18:55:46

+0

@AndyProwl每個人都知道你應該*神奇*知道爲什麼它不會工作! – Rapptz 2013-03-02 18:56:57

+0

@AndProwl謝謝你的注意。我會編輯我的帖子,以包括我遇到問題的方式。 – Mutoh 2013-03-02 18:56:57

回答

2

你的指標周圍出現錯誤的方式:

Type& operator() (int i, int j) { 
     return mat[j * columns + i]; 
    }; 

這裏,i顯然是列和j是行。然而,在循環語句中的參數出現相反的順序:

for (int i = 0; i < lanes; i++) { 
    for (int j = 0; j < cols; j++) { 
     map(i, j) = ...; // row then column 
    } 
} 

此外,析構函數應該使用delete[]而不是delete

+0

非常感謝,這是它。我仍然不明白爲什麼它沒有任何cin工作,但我被授予。 – Mutoh 2013-03-02 19:09:06

+0

順便說一句,我使用刪除而不是delete [],因爲在另一個應用程序中,只有在執行結束時使用delete []纔會崩潰 - 使用delete,即使在多次測試後,它也能正常工作。不過我會注意的。 – Mutoh 2013-03-02 19:10:13

相關問題