问题提出: 这个 template(模板)取得一个 type parameter(类型参数)T,但是它还有一个类型为 size_t 的参数——一个 non-type parameter(非类型参数)。 template<typename T, // template for n x n matrices of std::size_t n> // objects of type T; see below for info class SquareMatrix { // on the size_t parameter public: ... void invert(); // invert the matrix in place }; 问题解决: 修改一: template<typename T> // size-independent base class for class SquareMatrixBase { // square matrices protected: ... void invert(std::size_t matrixSize); // invert matrix of the given size ... }; template< typename T, std::size_t n> class SquareMatrix: private SquareMatrixBase<T> { private: using SquareMatrixBase<T>::invert; // avoid hiding base version of // invert; see Item 33 public: ... void invert() { this->invert(n); } // make inline call to base class }; // version of invert; see below // for why "this->" is here 修改二: template<typename T, std::size_t n> class SquareMatrix: private SquareMatrixBase<T> { public: SquareMatrix() // set base class data ptr to null, : SquareMatrixBase<T>(n, 0), // allocate memory for matrix pData(new T[n*n]) // values, save a ptr to the { this->setDataPtr(pData.get()); } // memory, and give a copy of it ... // to the base class private: boost::scoped_array<T> pData; // see Item 13 for info on }; // boost::scoped_array