我正在尝试编写一个普通的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