C++ 收到有关压缩结构的意外编译警告

C++ 收到有关压缩结构的意外编译警告,c++,C++,当我运行下面的代码片段时,得到了警告,不知道为什么。g++的版本为4.8.4 $ g++ -g -fPIC -std=c++11 -fpermissive te.cc te.cc:25:11: warning: ignoring packed attribute because of unpacked non-POD field ‘cMacAddr strArp::src_hw_mac’ [enabled by default] cMacAddr src_hw_mac;

当我运行下面的代码片段时,得到了警告,不知道为什么。g++的版本为4.8.4

$ g++ -g  -fPIC -std=c++11 -fpermissive te.cc
te.cc:25:11: warning: ignoring packed attribute because of unpacked non-POD field ‘cMacAddr strArp::src_hw_mac’ [enabled by default]
  cMacAddr src_hw_mac;
           ^
te.cc:27:11: warning: ignoring packed attribute because of unpacked non-POD field ‘cMacAddr strArp::dst_hw_mac’ [enabled by default]
  cMacAddr dst_hw_mac;
代码片段“te.cc”

#包括
#包括
#包括
#包括
使用名称空间std;
typedef无符号字符;
typedef无符号短uint16;
typedef无符号整数单元;
char_ethHdrPrintBuf[100];
类cMacAddr{
公众:
乌查尔地址[6];

Cmacadr(){for(int i=0;i
Cmacadr
有一个构造函数,因此它不能是一个POD。太多细节。

Cmacadr
有一个构造函数,因此它不能是一个POD。太多细节。

这是不言自明的。
Cmacadr
是一个“非POD”字段,并且未打包,因此
strArp
被迫忽略
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>属性。@chad:如果我删除
字段,并且未打包,因此
strArp
被迫忽略
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu属性((\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
根据结构的大小为我提供所需的结果。那么如何删除警告?要么从
cmacadr
中删除构造函数,要么同时提供
\uuuuuuuuuuuuuuuuuuuu
属性。如果是我,我会为
cmacadr
声明一个POD类。您可以通过除con>之外的其他方式将结构归零谢谢你的链接。但是有
\uuuuu属性((\uuuu packed\uuuu))
根据结构的大小为我提供所需的结果。那么如何删除警告?要么从
cmacadr
中删除构造函数,要么同时提供
\uuuuuuuuuuuuuuuuuuuu
属性。如果是我,我会为
cmacadr
声明一个POD类。您可以通过除con>之外的其他方式将结构归零结构者。
#include <iostream>
#include <utility>
#include <functional>
#include <string>

using namespace std;
typedef unsigned char uchar;
typedef unsigned short uint16;
typedef unsigned int uint;

char _ethHdrPrintBuf[100];

class cMacAddr {
public:
    uchar addr[6];
    cMacAddr() { for (int i=0; i<6; i++) addr[i] = 0;}
};

struct strArp {
    uint16 hw_type;
    uint16 proto_type;
    uchar  hw_size;
    uchar  proto_size;
    uint16 opcode;
    cMacAddr src_hw_mac;
    uint  src_proto_ipv4;
    cMacAddr dst_hw_mac;
    uint  dst_proto_ipv4;
} __attribute__((__packed__ )) ;
int main()
{
    cout << sizeof(strArp) << endl;
    return 0;
}