c++ - C/C++ 警告 : address of temporary with BDADDR_ANY Bluetooth library

标签 c++ c ubuntu bluetooth temporary

我在使用 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++ 中是不允许的。

您可以通过创建一个命名的临时变量并在其上使用 bacpybacmp 来解决这个问题:

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/

相关文章:

c - 为什么BSD queue.h LIST 叫做列表?

php - HipHop-Php 虚拟机和 auto_prepend

linux - ubuntu 13.10 中的 linsched 错误

c++ - 为什么 pread 接受偏移值时需要 lseek?

c++ - C++ 是否有现有方法来检查对象是否为派生类型对象?

c++ - 使用多态子类各自的方法

c - 在多维数组中,为什么取消引用一行的地址再次导致地址而不是值

c++ - 如何在 C 中访问 SOAP header

c++ - 使用 C 扫描 wifi 网络

c - "using host libthread_db library"不包括 gdb 调试器