0

我正在开发一个必须支持 IPv6 寻址方案的项目。这需要我将 winsock.h 替换为 winsock2.h 以进行 IP 版本独立调用。最初,我遇到了与重新定义相关的错误,我可以通过使用在线可用的解决方案来消除这些错误(放在;#include <winsock2.h>之前)。但是,我仍然收到如下错误:#include <windows.h>#define_WINSOCKAPI_

2>../include\obsocket.h(171) : error C2143: syntax error : missing '}' before 'constant'
2>../include\obsocket.h(171) : error C2059: syntax error : 'constant'
2>../include\obsocket.h(171) : error C2143: syntax error : missing ';' before '}'
2>../include\obsocket.h(171) : error C2238: unexpected token(s) preceding ';'
2>../include\obsocket.h(173) : error C2065: 'ShutdownType' : undeclared identifier
2>../include\obsocket.h(177) : error C2146: syntax error : missing ';' before identifier 'GetDescriptor'
2>../include\obsocket.h(177) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(177) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(177) : error C2065: '_sock' : undeclared identifier
2>../include\obsocket.h(179) : error C2065: '_timeout' : undeclared identifier
2>../include\obsocket.h(180) : error C2065: '_use_blocking_calls' : undeclared identifier
2>../include\obsocket.h(181) : error C2065: '_req_pending' : undeclared identifier
2>../include\obsocket.h(185) : error C2270: 'ToString' : modifiers not allowed on nonmember functions
2>../include\obsocket.h(187) : error C2059: syntax error : 'private'
2>../include\obsocket.h(189) : error C2146: syntax error : missing ';' before identifier '_sock'
2>../include\obsocket.h(189) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(189) : error C2086: 'int SOCK_TYPE' : redefinition
2>../include\obsocket.h(177) : see declaration of 'SOCK_TYPE'
2>../include\obsocket.h(189) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>../include\obsocket.h(189) : error C2371: '_sock' : redefinition; different basic types
2>../include\obsocket.h(199) : error C2371: '_use_blocking_calls' : redefinition; different basic types
2>../include\obsocket.h(200) : error C2371: '_timeout' : redefinition; different basic types
2>../include\obsocket.h(201) : error C2371: '_req_pending' : redefinition; different basic types
2>../include\obsocket.h(203) : error C2146: syntax error : missing ')' before identifier 'sock'
2>../include\obsocket.h(203) : error C2146: syntax error : missing ';' before identifier 'sock'
2>../include\obsocket.h(203) : error C2371: 'SOCK_TYPE' : redefinition; different basic types

我已确保在构建期间不会将 winsock.h 包含在任何地方(通过在构建期间启用显示包含)。

这是 obsocket.h 的代码:

#ifndef _obsocket_h
#define _obsocket_h

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
//#include <winsock.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif

class ObSocket {

public:

#ifdef _WIN32
typedef SOCKET SOCK_TYPE;
#else
typedef int SOCK_TYPE;
#endif

ObSocket();
~ObSocket();

enum ShutdownType { SD_INCOMING = 0, SD_OUTGOING = 1, SD_BOTH = 2 };

ObStatus Shutdown(ShutdownType);
ObStatus Close();
bool IsValid();

SOCK_TYPE GetDescriptor() { return _sock; }
void NonBlocking(int timeout);
inline int GetTimeout( )  { return _timeout; }
inline bool IsBlocking( ) { return _use_blocking_calls; }
inline int IncPendingReq(int i = 1) { _req_pending += i; return _req_pending; }
inline int DecPendingReq(int i = 1) { _req_pending -= i; return _req_pending; }
inline int GetPendingReq( )  { return _req_pending; }

ObString& ToString(ObString& strClass) const;

private:

SOCK_TYPE _sock;
ObInetAddress _addr;

char _my_addr[20];
in_port_t _my_port;
char _remote_addr[20];
in_port_t _remote_port;

bool    _use_blocking_calls;
int _timeout;
int _req_pending;

ObSocket(SOCK_TYPE sock);
void SetAddr(sockaddr_in& inAddr, const ObInetAddress& addr, in_port_t port);
void ReallocFileDescriptor();

ObSocket(const ObSocket&);
ObSocket& operator=(const ObSocket&);

static void Initialize();
static void Finalize();
};

谁能帮忙指出可能出了什么问题?

4

0 回答 0