2015-05-09 68 views
0

我想構建一個簡單的矩陣類。我的頭文件中的相關部分看起來是這樣的:矩陣類與std :: array

template <typename T> 
class matrix 
{ 
private: 
    unsigned int nrows; 
    unsigned int ncols; 
    std::array<std::array<T, ncols>, nrows> mat; 

public: 
    matrix(); 

    unsigned int getCols() const; 
    unsigned int getRows() const; 

}; 

的這裏的問題是,二維數組(稱爲墊)需要的行和列數。顯然,這不起作用,但我不知道如何解決這個問題。

我的源文件:

template <typename T> 
matrix<T>::matrix() : nrows(0), ncols(0) {} 

template <typename T> 
unsigned int matrix<T>::getCols() const { 
    return ncols; 
} 

template <typename T> 
unsigned int matrix<T>::getRows() const { 
    return nrows; 
} 

矩陣的初始化應該是這個樣子:

matrix<double> my_matrix; 
+2

使用'的std ::矢量''的std ::陣列'尺寸必須在編譯時是已知的。 – 101010

回答

1

你不能擁有的arraysize可變大小的論點。因此,您必須爲matrix類另有兩個模板參數。

template <typename T, size_t ROWS, size_t COLS> 
class matrix 
... 
+0

謝謝你的提示,我現在已經實現了std :: vector類,因爲需要push_back等。 – beginneR