C++ bool运算符==在typedef结构中
如何将布尔运算符==添加到结构C++ bool运算符==在typedef结构中,c++,struct,C++,Struct,如何将布尔运算符==添加到结构bd\u addr\u t?我在C++项目中使用这个文件。< /P> #ifndef APITYPES_H_ #define APITYPES_H_ #ifdef __GNUC__ #define PACKSTRUCT( decl ) decl __attribute__((__packed__)) #define ALIGNED __attribute__((aligned(0x4))) #else //msvc #define PACKSTRUCT( d
bd\u addr\u t
?我在C++项目中使用这个文件。< /P>
#ifndef APITYPES_H_
#define APITYPES_H_
#ifdef __GNUC__
#define PACKSTRUCT( decl ) decl __attribute__((__packed__))
#define ALIGNED __attribute__((aligned(0x4)))
#else //msvc
#define PACKSTRUCT( decl ) __pragma( pack(push, 1) ) decl __pragma( pack(pop) )
#define ALIGNED
#endif
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef signed short int16;
typedef unsigned long uint32;
typedef signed char int8;
typedef struct bd_addr_t
{
uint8 addr[6];
}bd_addr;
typedef bd_addr hwaddr;
typedef struct
{
uint8 len;
uint8 data[];
}uint8array;
typedef struct
{
uint8 len;
int8 data[];
}string;
#endif
我试图补充
bool operator==(const bd_addr_t& a) const
{
return (addr[0] == a.addr[0] && addr[1] == a.addr[1] && addr[2] == a.addr[2] && addr[3] == a.addr[3] && addr[4] == a.addr[4] && addr[5] == a.addr[5]);
}
但这会在编译过程中引发两个错误:
unknown type name 'bool' bool operator==(const bd_addr_t& a) const
expected ':', ',', ';', '}' or '__attribute__' before '==' token bool operator==(const bd_addr_t& a) const
您不能将此类函数添加到此C编译器也使用的标头中。其中一种方法是创建一个包装头,只在C++代码中使用,并添加独立函数(它不能是一种方法,因为它要求在结构中声明,使代码与C不兼容):
#包括
#ifndef_uucplusplus
这个错误只包含在C++代码中
#恩迪夫
布尔运算符==(常数bd_addr_t&a,常数bd_addr_t&b);
然后在一个.CPP文件中实现它,或者您可以在这个头中实现它,并使该函数<代码>内联
您真的在这个文件上使用C++编译器吗?看起来它不知道典型的C++特性。为什么在C++中使用<代码> TyPufFrase?你确定这是用C++编译的吗?IIRCuint8数据[]代码>是一个灵活的数组成员,在C++中不是标准的。这看起来像C代码,你确信编译器是编译为C++的吗?那里的文件有.c文件扩展名,所以我怀疑它们应该用C编译器编译。
#include <original_header.h>
#ifndef __cplusplus
#error This header is only to be included in C++ code
#endif
bool operator==(const bd_addr_t& a, const bd_addr_t& b);