C ++ Pointer to function call not in scope

I spent the last 3 hours trying to figure out what I'm doing wrong. I just need other views on this, I tried everything I can think of, even various random permutations in an attempt to get the compiler to tell me something useful.

This is where I am now:

the code:

class villain { public: villain(); //lots of other stuff bool Type1EnemyBlasts(); bool Type2EnemyBlasts(); bool (villain::*OnFire)(); }; villain::villain() { //lots of other stuff OnFire = &villain::Type1EnemyBlasts; } main { //lots and lots of other stuff villain *targets[100]; targets[0] = new villain(); if(targets[0]->*OnFire() == true) { //do stuff } } 

The error occurs when I call "target [0] → * OnFire ()", where it states that it is not declared in this area. It seems strange to me that I have to define "OnFire" with "& villain ::" when it is defined inside an attacker, but all the information I found suggests that it should be done this way, and really if I try differently, deflates a number of errors.

What is the correct syntax for calling the * OnFire () pointer, which belongs to target [0]?

+7
c ++ scope pointers reference function-pointers
source share
4 answers

The syntax for calling a pointer to a member function is pretty cruel. Here is the correct syntax:

 if ((targets[0]->*(targets[0]->OnFire))() == true) 

It is completely ugly. That's why the Parashift C ++ Frequently Asked Questions page when calling pointers to member functions recommends defining a macro for this purpose - and note that the author of these pages does not like macros.

 #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) ... if (CALL_MEMBER_FN(*targets[0],targets[0]->OnFire)() == true) 

Use a macro or use std::function as described in the answer.

+3
source share

Change

 if(targets[0]->*OnFire() == true) 

to

 if((targets[0]->*(targets[0]->OnFire))() == true) 

Since the function call operator () has a higher priority than ->* , the first form is interpreted as:

 if(targets[0]->*(OnFire()) == true) 

which, as you see, is not what you want.

+2
source share

There is no correct syntax. It would be terrible (see below)

Use std::function instead:

 #include <functional> struct villain { villain(); //lots of other stuff bool Type1EnemyBlasts() {return true;} bool Type2EnemyBlasts() {return false;} std::function<bool(villain&)> OnFire; }; villain::villain() { OnFire = &villain::Type1EnemyBlasts; } int main() { villain *targets[100]; targets[0] = new villain(); villain& v = *targets[0]; if (v.OnFire(v)) { return 1; } return 0; } 

Contrast with a recipe pointer to a member function:

 struct villain { villain(); //lots of other stuff bool Type1EnemyBlasts() {return true;} bool Type2EnemyBlasts() {return false;} bool (villain::*OnFire)(); }; villain::villain() { //lots of other stuff OnFire = &villain::Type1EnemyBlasts; } int main() { //lots and lots of other stuff villain *targets[100]; targets[0] = new villain(); villain& v = *targets[0]; if ((v.*v.OnFire)()) { return 1; } return 0; } 
+1
source share

Try: (targets[0]->*OnFire)()

It has some great info on function pointers: http://www.newty.de/fpt/fpt.html#defi

0
source share

All Articles