2017-06-06 87 views
-1

如何將布爾運算符==添加到結構bd_addr_t?我在C++項目中使用這個文件。bool operator == typedef struct

#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 
+0

你有沒有審查[比較運算符](http://en.cppreference.com/w/cpp /語言/ operator_comparison)? –

+0

你真的在這個文件上使用C++編譯器嗎?看起來它不知道典型的C++特性。 – bipll

+2

爲什麼在C++中使用'typedef struct'? – Slava

回答

0

不能添加這樣的功能到這個標題所使用的C編譯器爲好。其中一種方法可能是創建一個只用於您的C++代碼並添加獨立函數的包裝器頭文件(它不能是一種方法,因爲這需要在struct中聲明,並使該代碼與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); 

,然後實現它的.cpp文件或一個你可以在這個頭實現,而且使該功能inline