错误,不能使用类型为'void'的r值的地址。无法获取r值类型为 "void "的地址。

问题描述 投票:0回答:2

当我在MBED中使用类InterruptIn时,我开始收到这个错误。这是我在MBED中使用InterruptIn类的定义。fall 函数,我试图调用。

enter image description here

这是我的代码

   void Sensor::contador(int cont){
          cont++;
   }  
   int Sensor::medidaSensor(){
    //Se activa el watchdog:
       Timer timer;
       timer.start();
       int npulsos=0;
       while(1){
           //Cuenta los pulsos durante 5ms
           if (timer.read_ms() < 5)
           {
              vcomp.fall(&contador(npulsos)); // <= compilation error here
           }
           else{
               //Kick the watchdog to reset its timer
                watchdogTimer.kick();
           }
       }
       return npulsos;
   }
c++ function-pointers rvalue mbed
2个回答
2
投票

从您的 fall 定义

  • obj - 指向要调用成员函数的对象的指针。
  • 方法 - 指向要调用的成员函数的指针。

我想你是想。

vcomp.fall(this, &Sensor::contador);

即调用当前对象的contador方法。

但是请注意,这个方法不接受回调成员函数的参数,因此你不能调用你当前的contador方法:你必须在类中把count作为一个字段,或者使用不同的fall()签名,比如接受回调函数的签名。(我的C++ lamdas恐怕已经生疏了,所以我不确定你是否可以像在其他语言中那样使用一个带闭包的lambda来复制这个方法,或者这是否会导致lambda的寿命问题。)

在任何情况下,在 最新版本的文件 这个版本的fall方法被标记为废弃,而采用回调版本。


1
投票

这是调用fall函数的正确方法(根据你所附的图片),我们不知道这个函数做什么,但没关系。

  void Sensor::contador(int* cont){
          ++(*cont);
   }  
   int Sensor::medidaSensor(){
    //Se activa el watchdog:
       Timer timer;
       timer.start();
       int npulsos=0;
       while(1){
           //Cuenta los pulsos durante 5ms
           if (timer.read_ms() < 5)
           {
              vcomp.fall(this,Sensor::contador);
           }
           else{
               //Kick the watchdog to reset its timer
                watchdogTimer.kick();
           }
       }
       return npulsos;
   }
© www.soinside.com 2019 - 2024. All rights reserved.