C/C ++警告:BDADDR_ANY蓝牙库的临时地址 [英] C/C++ Warning: address of temporary with BDADDR_ANY Bluetooth library
问题描述
我在使用Ubuntu下的Bluetooth库的g ++和C/C ++程序的编译过程中遇到了一些问题.
I'm having some problems with g++ and the compiling process for a C/C++ program which use Bluetooth libraries under Ubuntu.
如果我使用gcc,它可以正常工作而不会发出警告;相反,如果我使用g ++,则会收到以下警告:
If i use gcc, it works fine with no warning; on the contrary, if i use g++ i get this warning:
警告:以临时地址为地址
warning: taking address of temporary
即使该程序可以正常运行并且可以正常工作.
even if the program compiles fine and it works.
报告错误的相关行是:
bdaddr_t *inquiry(){
// do some stuff..
bacpy(&result[mote++], BDADDR_ANY);
return result;
}
//...
void zeemote(){
while (bacmp(bdaddr, BDADDR_ANY)){
/..
}
}
在两种情况下,都涉及BDADDR_ANY.
In both the cases, BDADDR_ANY is involved.
我该如何解决此警告?
BDADDR_ANY是在bluetooth.h中定义的,例如:
BDADDR_ANY is defined in bluetooth.h like:
/* BD Address */
typedef struct {
uint8_t b[6];
} __attribute__((packed)) bdaddr_t;
#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
推荐答案
(&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
构造一个临时对象并使用其地址.在C ++中是不允许的.
Constructs a temporary object and uses its address. This isn't allowed in C++.
您可以通过创建命名的临时变量并在其上使用bacpy
和bacmp
来解决此问题:
You can fix this by creating a named temporary variable and using bacpy
and bacmp
on it:
bdaddr_t tmp = { };
bacpy(&result[mote++], &tmp);
和
while (bacmp(bdaddr, &tmp)) {
//
}
这篇关于C/C ++警告:BDADDR_ANY蓝牙库的临时地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!