所以我有这个“示例”代码
#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
#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 */
packets.cpp:在成员函数'void _Packet :: Analyze(const u_char *)'中:
packets.cpp:20:3:错误:未在此范围内声明“UDP”
packets.cpp:20:10:错误:'_UDP'未在此范围内声明
packets.cpp:20:15:错误:在')'令牌之前预期的主要表达式
packets.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:错误:'class _Packet'没有名为'Get_UDP_Source_Port'的成员
main.cpp:127:45:错误:'class _Packet'没有名为'Get_UDP_Destination_Port
的成员我不知道为什么会出现此错误。它应该正常工作