错误:无法在“ sensor.cpp”中获取类型为“ void”的右值的地址,行:50,列:26

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

当我在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));
           }
           else{
               //Kick the watchdog to reset its timer
                watchdogTimer.kick();
           }
       }
       return npulsos;
   }
c++ rvalue mbed
2个回答
0
投票

这是调用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(&npulsos,contador);
           }
           else{
               //Kick the watchdog to reset its timer
                watchdogTimer.kick();
           }
       }
       return npulsos;
   }

0
投票

根据您的fall定义

  • obj-指向要在其上调用成员函数的对象的指针>
  • 方法-调用要调用的成员函数
  • 我认为您想要:

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

即在当前对象上调用contador方法。

但是请注意,此方法不接受contador的参数,因此您不能调用当前的contador方法:您必须将count改为类中的一个字段,例如使用不同的fall()签名,例如接受回调函数的签名。 (恐怕我的C ++ lamdas生锈了,所以我不确定是否可以像其他语言一样使用带闭包的lambda复制此代码,否则是否会导致lambda的生存期出现问题。)] >

© www.soinside.com 2019 - 2024. All rights reserved.