错误C2761成员函数重新声明不允许 [英] error C2761 member function redeclaration not allowed

查看:2588
本文介绍了错误C2761成员函数重新声明不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为类编写专业化时遇到问题(错误C2761)。我的类如下:

 类打印{
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;
};

我有一个类 QuickPrint 打印 class:

  class QuickPrint:public Print {
...
};

当我尝试为 startPrint 方法:

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

模板<> //< = C2716 error here here
void QuickPrint :: startPrint< fontB>(void)
{
/// implementation
}
解决方案

/ div>

QuickPrint没有名为startPrint的模板成员函数,因此专业化QuickPrint :: startPrint是一个错误。如果你在QuickPrint中重复模板定义,那么专业化是可以的:

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

模板<> //< = C2716错误在这里
void QuickPrint :: startPrint< Print :: fontA>(void)
{
/// implementation
}

但是如果这里的目标是能够以多态方式调用QuickPrint,无法将打印标记为虚拟。


I've encountered a problem(error C2761) while writing specializations for a class. My classes are as follows:

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;
};

I have a class QuickPrint which inherits the Print class:

class QuickPrint : public Print {
       ...
};

The error occurs when I try to write specializations for the startPrint method:

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

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

The error appears for the remaining specializations as well.

解决方案

QuickPrint does not have a template member function named startPrint, so specializing QuickPrint::startPrint is an error. If you repeat the template definition in QuickPrint the specializations are okay:

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

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

But if the goal here is to be able to call into QuickPrint polymorphically, you're in trouble, because the template function in Print can't be marked virtual.

这篇关于错误C2761成员函数重新声明不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆