2017-06-06 15 views
-1

bool演算子==を構造体に追加するにはどうすればよいですかbd_addr_t?私はこのファイルをC++プロジェクト内で使用しています。bool演算子== typedef構造体内

#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]); 
    } 

を追加しようとしましたが、これはコンパイル時に2つのエラーがスローされます。

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/language/operator_comparison)? –

+0

このファイルで実際にC++コンパイラを使用していますか?典型的なC++機能を認識していないように見えます。 – bipll

+2

C++でなぜ 'typedef struct'を使用しますか? – Slava

答えて

0

あなたにも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); 
その後、

、あなたが検討したの.cppファイルのいずれかでそれを実装するか、このヘッダにそれを実装し、その関数にinline

関連する問題