Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误C2761不允许成员函数重新声明_C++_Templates_Specialization - Fatal编程技术网

C++ 错误C2761不允许成员函数重新声明

C++ 错误C2761不允许成员函数重新声明,c++,templates,specialization,C++,Templates,Specialization,我在为类编写专门化时遇到了一个问题(错误C2761)。我的课程如下: class Print{ public: typedef class fontA; typedef class fontB; typedef class fontC; typedef class fontD; template<class T> void startPrint(void) { return; }; virtual bool isValidDo

我在为类编写专门化时遇到了一个问题(错误C2761)。我的课程如下:

class Print{
public:
    typedef class fontA;
    typedef class fontB;
    typedef class fontC;
    typedef class fontD;

    template<class T>
    void startPrint(void) { return; };
    virtual bool isValidDoc(void) = 0;
};
尝试为
startPrint
方法编写专门化时出错:

template<>        // <= C2716 error given here
void QuickPrint::startPrint<fontA>(void)
{
      /// implementation
}

template<>        // <= C2716 error given here
void QuickPrint::startPrint<fontB>(void)
{
     /// implementation
}

template/您的问题是这一行:

template<class T> 
void startPrint(void) { return; }; 
模板
void startPrint(void){return;};
您已经提供了一个函数体,但是尝试重新定义它(使用专门化),这是行不通的

拆下该功能体,它应该可以工作


要定义默认行为,请为模板参数
T
提供一个默认值,然后为该参数添加一个专门化。

QuickPrint没有名为startPrint的模板成员函数,因此专门化QuickPrint::startPrint是一个错误。如果在“快速打印”中重复模板定义,则可以进行专门化:

class QuickPrint : public Print {
   template<class T>
    void startPrint(void) { return; };
 };

template<>        // <= C2716 error given here
void QuickPrint::startPrint<Print::fontA>(void)
{
      /// implementation
}
类快速打印:公共打印{
模板
void startPrint(void){return;};
};
模板//尝试以下操作:

class QuickPrint : public Print {
    template<class T>
    void startPrint(void) { Print::startPrint<T>(); };
};
类快速打印:公共打印{
模板
void startPrint(void){Print::startPrint();};
};

谢谢,将默认实现添加到派生类中是有效的。这似乎很危险。如果现在
Print
的某个函数调用
startPrint
,他编写的专门化就不会被选中,因为他没有专门化Print的成员函数模板;它不是多态的。
class QuickPrint : public Print {
    template<class T>
    void startPrint(void) { Print::startPrint<T>(); };
};