在QT中使用系统的close函数关闭设备文件,在编译的时候,出现错误及解决方法。
QT中的实现函数如下
#include <unistd.h> /*Unix 标准函数定义*/
#include <fcntl.h> /*文件控制定义*/
void MainWindow::on_pushButton_clicked()
{
int keys_fd;
struct input_event t;
keys_fd = open("/dev/input/event0", O_RDWR);
if(keys_fd<=0)
{
printf("open %d device error!\n",keys_fd);
exit(0);
}
close(keys_fd);
}
编译的时候,出现如下错误:
mainwindow.cpp:68: error: no matching function for call to‘MainWindow::close(int&)’
原因:系统的close函数(带有参数,用来关闭设备文件,在<unistd.h>中定义)和QT的close函数(不带参数,用来关闭QT中的窗口)的名字相同,在QT的MainWindow类中,由于作用域的限制,默认只能使用QT的close函数(同名的系统的close函数被屏蔽),而QT的close函数不能指定参数,因此会出现这样的编译错误。
解决方法:将“close(keys_fd)”修改为“::close(keys_fd)”,问题解决。加上“::”作用域符号后,系统的close函数的作用域被扩大,可以在MainWindow类中使用。