2010-06-22 57 views
0

我有一個矩陣類,它可以被用來訪問它作爲模板參數的行和列引用類型有些圓度。同時,行和列類型傳遞它們表示的矩陣類型。我有我的模板類定義

有什麼辦法打破這個循環?下面是一些說明性的代碼片段:

#include "MatrixConcreteType.h" 
template <class MatrixType> 
class rowType <MatrixType> 
{...} 

而在矩陣文件:

#include "VectorTypes.h" 
template <class row_t, class col_t> 
class Matrix 
{...} 

我想我可以嘗試有條件的extern?

+0

你爲什麼試圖做到這一點,而不是根據底層數據類型(例如,數據類型)定義這些類中的一個(或兩個)。 int,double等)?對於這個問題,數據類型在哪裏定義/聲明? – tlayton 2010-06-22 17:55:32

+0

行和列類型如何變化以提供不同的矩陣? 它是行和列改變的類型,還是隻是實例? – 2010-06-22 19:16:03

回答

1

如果我理解正確你的問題,你都試圖定義矩陣爲模板,採取行類型和列類型,然後行類型(我假設也columnType)基質型的模板。我最大的問題是:實際數據在哪裏?在某種程度上,我認爲這個矩陣實際上應該分解爲一系列整數,或者雙精度或者字符,或者反轉指向布爾指針的迭代器等等。哪裏是?

你圓似乎是試圖讓太多架構的症狀。找出你想實際存儲在數據上,進行類數據類型的模板參數,然後讓所有其他相關的類或者主類,或數據類型的模板參數,以及哪個類。例如:

template <class DataType> 
class Matrix{ 
    //store the data in here: a DataType [], or vector<DataType>, or something 
} 

和:

template <class MatrixType> 
class Row{ 
    //has something which refers back to the original matrix of type MatrixType 
} 

template <class DataType> 
class Row{ 
    //has something which refers back to the original matrix of type Matrix<DataType> 
} 

我建議使用上述選擇的第二,因爲它使被更容易地指Row類中Matrix類,如:

template <class DataType> 
Row<DataType> Matrix::getRow(int index){ 
    //return an instance of Row<DataType> containing the appropriate row 
}