bool operator== within typedef struct
How can I add the bool operator== to the struct bd_addr_t
? I'm using this file within a C++ project.
#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
I tried to add
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]);
}
but this throws two errors during compilation:
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
You cannot add such function into this header which is used by C compiler as well. One of the way could be to create a wrapper header that you use only in you C++ code and add standalone function (it cannot be a method as that would require it's declaration within struct and make that code incompatible with 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);
and then implement it in one of .cpp files or you can implement it in this header and make that function inline
上一篇: 重载!= as ==否定
下一篇: 布尔运算符==在typedef结构中