1

所以我有这个“示例”代码

数据包.h

#ifndef PACKETS_H_
#define PACKETS_H_

#include <winsock2.h>

typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;

/* Some code here */

typedef struct _UDP
{
    u_short UDP_Source_Port;
    u_short UDP_Destination_Port;
    u_short UDP_Total_Length;
    u_short UDP_CheckSum;
} _UDP;

class _Packet
{
    private:
        _IPv4_Header* IPv4_Header;
        _UDP* UDP;
        _TCP* TCP;
    public:
        _Packet(void);
        ~_Packet(void);
        void Analyze(const u_char* Packet_Data);

            /* Some code here */

        u_short Get_UDP_Source_Port(void) { return UDP->UDP_Destination_Port; }
        u_short Get_UDP_Destination_Port(void) { return UDP->UDP_Destination_Port; }
        u_short Get_UDP_Total_Length(void) { return UDP->UDP_Total_Length; }
        u_short Get_UDP_CheckSum(void) { return UDP->UDP_CheckSum; }
};

#endif

数据包.cpp

#include "Packets.h"
#include <pcap.h>

_Packet::_Packet(void)
{

}

_Packet::~_Packet(void)
{

}

void _Packet::Analyze(const u_char* Packet_Data)
{
    IPv4_Header = (_IPv4_Header*) Packet_Data;
    if (0x06 == Get_IPv4_Protocol())
        TCP = (_TCP*) Packet_Data + (int)Get_IPv4_Header_Length() / 4;
    else if (0x11 == Get_IPv4_Protocol())
        UDP = (_UDP*) Packet_Data + (int)Get_IPv4_Header_Length() / 4;
}

/* Some code here */

packet.cpp:在成员函数'void _Packet::Analyze(const u_char*)'中:

packet.cpp:20:3:错误:“UDP”未在此范围内声明

packet.cpp:20:10:错误:未在此范围内声明“_UDP”

packet.cpp:20:15: 错误: ')' 标记之前的预期主表达式

packet.cpp:20:17: 错误: 预期 ';' 在'Packet_Data'之前

主文件

/* Some code here */

_Packet Packet;
cout << "  Source port: " << Packet.Get_UDP_Source_Port() << endl;
cout << "  Destination port: " << Packet.Get_UDP_Destination_Port() << endl;

/* Some code here */

main.cpp:126:40:错误:“类 _Packet”没有名为“Get_UDP_Source_Port”的成员

main.cpp:127:45:错误:“类 _Packet”没有名为“Get_UDP_Destination_Port”的成员

我不知道为什么会出现这个错误。它应该可以正常工作

4

0 回答 0