50332

C++ Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”

Question:

I work with TaskScheduler COM, this is my code:

<pre class="lang-cpp prettyprint-override">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:

<blockquote>

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

</blockquote>

What is my mistake and how to correct it?

Answer1:

The correct definition for a pointer to <strong>member</strong> 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); }

Answer2:

I am guessing that get_StopIfGoingOnBatteries is a member function ofITaskSettings. Such a function cannot be used when the expected argument type is FuncOfBoll. You'll need to create a wrapper function and use it.

ITaskSettings* currentSetttings = NULL; HRESULT GetBoolWrapper(_Out_ VARIANT_BOOL* b) { return currentSetttings->get_StopIfGoingOnBatteries(b); } void test(ITaskSettings* settings) { currentSetttings = settings; bool b = GetBool(GetBoolWrapper); }

Answer3:

<sup>This is not really an answer, the question as written doesn't admit an answer, but this is too long & detailed for comment.</sup>

You don't show a full examples, so there has to be <strong>guesswork</strong>.

Apparently, judging by the error message, settings is a pointer to class type object, where that class has a member function get_StopIfGoingOnBatteries.

And apparently, judging by the use of -> operator, it's a non-static member function. You could still use -> if it were static, but that would be unnatural. So let's say it's a non-static member function.

Then you can't easily form a raw pointer to function that calls that member function, because you need a this-pointer for the call. It could just use a dummy object, if creation of such object is cheap, or it could use a global instance or pointer to instance. But better change something in your design, and/or explain more clearly what you want.

Recommend

  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Question about instantiating object
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • How to use carriage return with multiple line?
  • Email verification using google app script and google forms
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Control modification in presentation layer
  • Javascript simulate pressing enter in input box
  • How reduce the height of an mschart by breaking up the y-axis
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Build own AppleScript numerical error handling
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Adding custom controls to a full screen movie
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Google cloud sdk not working when python points python3
  • Apache 2.4 - remove | delete | uninstall
  • XCode can't find symbols for a specific iOS library/framework project
  • Confusion with PayPal's monthly billing cycle
  • Is there a mandatory requirement to switch app.yaml?
  • Proper way to use connect-multiparty with express.js?
  • Hits per day in Google Big Query
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal