无法将对象传递给函数。

huangapple go评论105阅读模式
英文:

Can't pass object into function

问题

所以我正在尝试制作一个程序,用于提供树莓派和加速度计/陀螺仪之间的接口。我需要这个函数尽可能频繁地更新,并且我想设置一个定时器来运行它。

以下是我的代码:

  1. void updateGyro(Gyro gyro) {
  2. gyro.update();
  3. }
  4. int main() {
  5. if (gpioInitialise() < 0)
  6. return -1;
  7. // 创建新的陀螺仪对象
  8. Gyro gyro;
  9. // 设置一个定时器,每10毫秒更新一次
  10. gpioSetTimerFuncEx(0, 10, updateGyro, &gyro);
  11. time_sleep(10);
  12. gpioTerminate();
  13. }

但这似乎不起作用,我得到了错误:

  1. gyro.cpp: In function int main():
  2. gyro.cpp:113:31: error: invalid conversion from void (*)(Gyro) to gpioTimerFuncEx_t {aka void (*)(void*)} [-fpermissive]
  3. 113 | gpioSetTimerFuncEx(0, 10, updateGyro, &gyro);
  4. | ^~~~~~~~~~
  5. | |
  6. | void (*)(Gyro)
  7. In file included from gyro.cpp:2:
  8. /usr/include/pigpio.h:3751:55: note: initializing argument 3 of int gpioSetTimerFuncEx(unsigned int, unsigned int, gpioTimerFuncEx_t, void*)
  9. 3751 | unsigned timer, unsigned millis, gpioTimerFuncEx_t f, void *userdata);
  10. | ~~~~~~~~~~~~~~~~~~^

我尝试将gyro.update函数直接传递给函数,但也没有起作用。有人知道如何修复这个问题吗?

这是我想要使用的函数的文档:
无法将对象传递给函数。

英文:

So I'm trying to make a program to provide a interface between a raspberry pi and a accelerometer/gyroscope. I need this function to update as often as possible and I want to set a timer for when it will run.

And here is my code:

  1. void updateGyro(Gyro gyro) {
  2. gyro.update();
  3. }
  4. int main() {
  5. if (gpioInitialise() &lt; 0)
  6. return -1;
  7. // Creates new gyro object
  8. Gyro gyro;
  9. // Sets a timer to update every 10 milliseconds
  10. gpioSetTimerFuncEx(0, 10, updateGyro, &amp;gyro);
  11. time_sleep(10);
  12. gpioTerminate();
  13. }

But this doesn't seem to work and I get the error:

  1. gyro.cpp: In function int main():
  2. gyro.cpp:113:31: error: invalid conversion from void (*)(Gyro) to gpioTimerFuncEx_t {aka void (*)(void*)} [-fpermissive]
  3. 113 | gpioSetTimerFuncEx(0, 10, updateGyro, &amp;gyro);
  4. | ^~~~~~~~~~
  5. | |
  6. | void (*)(Gyro)
  7. In file included from gyro.cpp:2:
  8. /usr/include/pigpio.h:3751:55: note: initializing argument 3 of int gpioSetTimerFuncEx(unsigned int, unsigned int, gpioTimerFuncEx_t, void*)
  9. 3751 | unsigned timer, unsigned millis, gpioTimerFuncEx_t f, void *userdata);
  10. | ~~~~~~~~~~~~~~~~~~^

I've tried passing in the gyro.update function in directly into the function but that also didn't work. Does anyone know how to fix this?

Here is the documentation for the function I want to use:
无法将对象传递给函数。

答案1

得分: 6

void*不是任何数量或类型参数的占位符。它确切地表示单个指向未知类型的指针。updateGyro 不接受类型为 void* 的参数,它接受类型为 Gyro 的参数。

您需要更改您的函数以接受 void*,如下所示:

  1. void updateGyro(void* arg) {
  2. Gyro* gyro = static_cast<Gyro*>(arg);
  3. gyro->update();
  4. }
英文:

void* is not a placeholder for any number of any arguments. It means exactly that, single pointer to unknown type. updateGyro doesn't accept argument of type void*, it accepts argument of type Gyro.

You need to change your function to accept void* instead:

  1. void updateGyro(void* arg) {
  2. Gyro* gyro = static_cast&lt;Gyro*&gt;(arg);
  3. gyro-&gt;update();
  4. }

huangapple
  • 本文由 发表于 2023年6月22日 20:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定