小编use*_*913的帖子

C++指向函数调用的指针不在范围内

我花了最后3个小时试图找出我做错了什么.我只需要另外一些眼睛,我已经尝试了我能想到的一切,甚至各种随机排列,试图让编译器告诉我一些有用的东西.

这是我现在的位置:

码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

当我调用"targets [0] - >*OnFire()"时会发生错误,其中声明它未在此范围内声明.当我在恶棍构造函数中定义它时,我必须用"&villain ::"来定义"OnFire",这让我觉得很奇怪,但是我发现的所有信息都表明它必须以这种方式完成,而且确实它会搅动如果我不这样做,就会发生过多的错误.

调用属于目标[0]的*OnFire()指针的正确语法是什么?

c++ pointers scope function-pointers reference

7
推荐指数
1
解决办法
190
查看次数

标签 统计

c++ ×1

function-pointers ×1

pointers ×1

reference ×1

scope ×1