我在使用 g++ 和在 Ubuntu 下使用蓝牙库的 C/C++ 程序的编译过程时遇到了一些问题。
如果我使用 gcc,它可以正常工作,没有任何警告;相反,如果我使用 g++,我会收到此警告:
warning: taking address of temporary
即使程序编译正常并且可以运行。
报错涉及的线路有:
bdaddr_t *inquiry(){
// do some stuff..
bacpy(&result[mote++], BDADDR_ANY);
return result;
}
//...
void zeemote(){
while (bacmp(bdaddr, BDADDR_ANY)){
/..
}
}
在这两种情况下,都涉及 BDADDR_ANY。
我该如何解决这个警告?
BDADDR_ANY 在 bluetooth.h 中定义如下:
/* 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++ 中是不允许的。
您可以通过创建一个命名的临时变量并在其上使用 bacpy
和 bacmp
来解决这个问题:
bdaddr_t tmp = { };
bacpy(&result[mote++], &tmp);
和
while (bacmp(bdaddr, &tmp)) {
//
}
关于c++ - C/C++ 警告 : address of temporary with BDADDR_ANY Bluetooth library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9751710/