我正在嘗試編寫一個普通的Matrix類,使用C++模板嘗試刷新我的C++,並且向同伴編碼器解釋某些內容。使用C++模板類的簡單矩陣示例
這是我迄今爲止索姆:
template class<T>
class Matrix
{
public:
Matrix(const unsigned int rows, const unsigned int cols);
Matrix(const Matrix& m);
Matrix& operator=(const Matrix& m);
~Matrix();
unsigned int getNumRows() const;
unsigned int getNumCols() const;
template <class T> T getCellValue(unsigned int row, unsigned col) const;
template <class T> void setCellValue(unsigned int row, unsigned col, T value) const;
private:
// Note: intentionally NOT using smart pointers here ...
T * m_values;
};
template<class T> inline T Matrix::getCellValue(unsigned int row, unsigned col) const
{
}
template<class T> inline void Matrix::setCellValue(unsigned int row, unsigned col, T value)
{
}
我卡上的構造函數,因爲我需要分配一個新的[] T,它看起來像它需要一個模板方法 - 然而, ,我不確定我以前是否已經過模板化的ctor。
我該如何實現ctor?
使用複選標記不要忘記接受以前問題的答案。請參閱*上的常見問題解答*「如何在此提問?」*瞭解更多詳情。 – 2010-04-20 23:20:11
你對'getCellValue'和'setCellValue'的聲明不正確 - 你不需要(也不能)在它們前面有模板。 另外,當你想在類外定義它們時,它需要讀取'template inline T Matrix :: getCellValue(unsigned int row,unsigned col)const' –
rlbond
2010-04-20 23:34:13
@rlbond:謝謝你指出。我想我的C++比我想象的更生鏽...... – skyeagle 2010-04-21 00:14:16