如何定义模板类构造函数的单独实现

问题描述 投票:0回答:1

我正在使用 C++ 中的旧代码,该代码使用矩阵类的自定义实现。

矩阵类被实现为模板类。声明看起来像这样

/**********************************************************************/
/*** TMatrix private **************************************************/
/**********************************************************************/
//
template <class X> class TMatrix {

private :

long min_adr_row; 
long max_adr_row; 


/**********************************************************************/
/*** TMatrix public ***************************************************/
/**********************************************************************/
//
public:
   
long     row;                        
long     col;                        

X     **data;                        


 TMatrix<X> (const long & m = 0,              /*** Konstruktor      ***/
             const long & n = 0);
 TMatrix<X> (const TMatrix<X> & A);           /*** Copy-Konstruktor ***/
~TMatrix<X> (void);                           /*** Destruktor       ***/


/**********************************************************************/
/*** overloaded operators *******************************************/
/**********************************************************************/
//
X *        & operator [] (const long & index)   const; /*** []      ***/
TMatrix<X> & operator =  (const TMatrix<X> & A);       /***  =      ***/

TMatrix<X>   operator +  (const TMatrix<X> & A) const; /*** C = A+B ***/
TMatrix<X>   operator -  (const TMatrix<X> & A) const; /*** C = A-B ***/
TMatrix<X>   operator *  (const TMatrix<X> & A) const; /*** C = A*B ***/

TMatrix<X> & operator += (const TMatrix<X> & A);       /*** B = B+A ***/
TMatrix<X> & operator -= (const TMatrix<X> & A);       /*** B = B-A ***/
TMatrix<X> & operator *= (const TMatrix<X> & A);       /*** B = B*A ***/

TMatrix<X>   operator *  (const double a)       const; /*** B = A*a ***/
int          operator == (const TMatrix<X> & A) const; /*** B == A  ***/

// ... other methods

};

此代码包含在头文件 TMatrix.h 中。

实现包含在同一个头文件中。我在第一个构造函数的定义中遇到错误,当前如下所示:

template <class X> TMatrix<X>::TMatrix<X>(const long & m, const long & n) {


// .. do stuff where 'data' and 'X' are used


return;
}

该项目之前使用 C++ 11 运行,代码编译时没有错误。

但是,对于较新的 C++ 版本,此代码会根据不同版本抛出各种错误(无法编译)。

例如,在 C++ 14 下,我收到编译器错误 C2988 无法识别的模板声明/定义。

我怀疑这个构造函数的定义和/或类本身的声明可能存在本质上的错误。

我尝试将定义的第一行替换为

template <class X> TMatrix(const long & m, const long & n) {

这对我来说似乎更合乎逻辑,但它给出了语法错误:“无法定义演绎指南”。

我也不明白为什么 template 关键字必须在构造函数定义中使用,而不是在类内的声明中使用。

我现在正在考虑简单地用两个单独的 int 和 double 的“正常”构造函数替换这个模板构造函数,这是该类在代码中使用的唯一类型。但我仍然想了解导致此错误的原因以及为什么它在 C++ 11 中运行良好。

希望有人帮忙,谢谢。

c++ templates constructor declaration definition
1个回答
0
投票

构造函数的定义只能在双冒号之前有模板参数:

template <class X> 
TMatrix<X>::TMatrix(const long & m, const long & n) {
    // .. do stuff where 'data' and 'X' are used
    return;
}

Clang 在错误中告诉你这一点:

<source>:49:32: error: out-of-line constructor for 'TMatrix' cannot have template arguments
   49 | template <class X> TMatrix<X>::TMatrix<X>(const long & m, const long & n) {
      |   
© www.soinside.com 2019 - 2024. All rights reserved.