模板函数实现中的错误 C2975 [英] Error C2975 in a template function implementation

查看:62
本文介绍了模板函数实现中的错误 C2975的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过类似的问题,但没有提供足够的细节.所以我试图用更好的代码示例重新表述它.所以在这里.

I've asked a similar question already but did not provide enough details. So I'm trying to re-phrase it with a better code sample. So here it is.

首先,我使用的是 Visual Studio 2008 C++ 编译器,我正在尝试实现一个模板来调用类的成员方法.

First off I'm using Visual Studio 2008 C++ compiler and I'm trying to implement a template to call a member method of a class.

模板声明如下:

typedef void (*ON_CALL)(HWND);

template <typename CLASS, ON_CALL F>
class CMyTemplate
{
public:
    static void onProcess(CLASS* pDlg)
    {
        pDlg->SetTimer(100, 1, _onTimer);
    }

private:
    static void WINAPI _onTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
    {
        KillTimer(hwnd, idEvent);

        CLASS* pDlg = (CLASS*)CDialog::FromHandle(hwnd);
        if(pDlg)
        {
            pDlg->F(hwnd);
        }
    }
};

它实际上是这样称呼的:

And it's actually called like so:

void CMyDialog::SomeMethod()
{
    CMyTemplate<CMyDialog, OnCall>::onProcess(this);    //error C2975
}

void CMyDialog::OnCall(HWND hWnd)
{
    //Do work ...
}

但是我上面标记的那一行给了我这个错误:

But that line that I marked above gives me this error:

错误 C2975:'F':'CMyTemplate' 的模板参数无效,预期的编译时常量表达式

error C2975: 'F' : invalid template argument for 'CMyTemplate', expected compile-time constant expression

推荐答案

function OnCall 是类成员,它隐含了一个 this 指针,所以它不会匹配声明

function OnCall is class member which implicit a this pointer, so it will not match the declaration

这篇关于模板函数实现中的错误 C2975的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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