错误:参数中无效地使用了无效表达式

Error: invalid use of void expression in parameter

本文关键字:无效 表达式 参数 错误      更新时间:2023-10-16

由于timer_start函数,我的目标是每 1000 毫秒重复一次循环函数。

但一切都没有按计划进行:

g++ -DDMP_FIFO_RATE=9 -Wall -g -O2 -c -o demo_raw.o demo_raw.cpp demo_raw.cpp:在函数 'int main((' 中: demo_raw.cpp:66:33: 错误: 无效使用无效表达式 timer_start(loop((, 1000(; ^ make: *** [: demo_raw.o] 错误 1

构建失败(退出值 2,总时间:26 秒(

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include <wiringPi.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
void timer_start(std::function<void(void)> func, unsigned int interval)
{
std::thread([func, interval]() {
while (true)
{
func();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
}).detach();
}
void loop() 
{
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
printf("ax: %6hdtay: %6hdtaz: %6hdngx: %6hdtgy: %6hdtgz: %6hdnnn",ax,ay,az,gx,gy,gz);   
}
int main()
{
setup();
for (;;) 
timer_start(loop(), 1000); 
}

提前感谢您的帮助!

这里

timer_start(loop(), 1000); 

您将返回loop作为第一个参数传递给timer_start。但是loop返回void(又名什么都没有(,所以这没有任何意义。

您可能希望传递函数而不是调用它的结果,如

timer_start(loop, 1000); 

您的timer_start期望实际函数作为其第一个参数,而不是调用该函数返回的值(就像您包含的()一样(。

只需删除这些括号:

int main()
{
setup();
for (;;) 
timer_start(loop, 1000); 
}