C/C++ 警告:带有 BDADDR_ANY 蓝牙库的临时地址

2022-01-04 00:00:00 ubuntu c bluetooth c++ temporary

我在使用 g++ 和在 Ubuntu 下使用蓝牙库的 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:

警告:取临时地址

即使程序编译正常并且可以运行.

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.

我该如何解决这个警告?

How can i solve this warning?

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++.

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

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)) {
    //
}

相关文章