C ++ Visual Studio 2015“非标准语法;使用'&'创建指向成员的指针“ [英] C++ Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”

查看:3730
本文介绍了C ++ Visual Studio 2015“非标准语法;使用'&'创建指向成员的指针“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TaskScheduler COM,这是我的代码:

  typedef HRESULT(* FuncOfBoll )(_ Out_VARIANT_BOOL * b); 

static bool GetBool(FuncOfBoll func)
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr = func(& b);
if(FAILED(hr))return FALSE;
return b == VARIANT_TRUE;
}

void test(ITaskSettings * settings)
{
bool b = GetBool(settings-> get_StopIfGoingOnBatteries); //< =这里的错误
// ...
}

我收到以下错误:


错误C3867'ITaskSettings :: get_StopIfGoingOnBatteries':非标准
语法;使用'&'创建指向成员的指针


我的错误是什么以及如何纠正?

解决方案

成员函数的指针的正确定义是:

  typedef HRESULT(ITaskSettings :: * FuncOfBoll)(_ Out_ VARIANT_BOOL * b); 

然后,您应该将指针传递给对象实例,以便函数 GetBool

  static bool GetBool(ITaskSettings * setting,FuncOfBoll func)
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr =(setting-> * func)(& b);
...
}



 模板< class C> 
static bool GetBool(C * p,HRESULT(C :: * func)(_ Out_VARIANT_BOOL *))
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr =(p-> * func)(& b);


$ b b

  void test(ITaskSettings * settings)
{
currentSetttings = settings;
bool b = GetBool(settings,& ITaskSettings :: mb_function);
}


I work with TaskScheduler COM, this is my code:

typedef HRESULT(*FuncOfBoll)(_Out_ VARIANT_BOOL* b);

static bool GetBool(FuncOfBoll func)
{
    VARIANT_BOOL b = VARIANT_FALSE;
    HRESULT hr = func(&b);
    if (FAILED(hr)) return FALSE;
    return b == VARIANT_TRUE;
}

void test(ITaskSettings* settings)
{
    bool b = GetBool(settings->get_StopIfGoingOnBatteries); // <= The error here
    // ...
}

and I get the following error:

Error C3867 'ITaskSettings::get_StopIfGoingOnBatteries': non-standard syntax; use '&' to create a pointer to member

What is my mistake and how to correct it?

解决方案

The correct definition for a pointer to member function is:

typedef HRESULT(ITaskSettings::*FuncOfBoll)(_Out_ VARIANT_BOOL* b);

Then, you should pass the pointer to the object instance to function GetBool:

static bool GetBool(ITaskSettings* setting, FuncOfBoll func)
{
    VARIANT_BOOL b = VARIANT_FALSE;
    HRESULT hr = (setting->*func)(&b);
    ...
}

Or, with template:

template<class C>
static bool GetBool(C* p, HRESULT(C::*func)(_Out_ VARIANT_BOOL*))
{
    VARIANT_BOOL b = VARIANT_FALSE;
    HRESULT hr = (p->*func)(&b);
    ...
}

Invocation:

void test(ITaskSettings* settings)
{
    currentSetttings = settings;
    bool b = GetBool(settings, &ITaskSettings::mb_function);
}

这篇关于C ++ Visual Studio 2015“非标准语法;使用'&amp;'创建指向成员的指针“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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