C++ 重定义头文件 (winsock2.h)
How do I prevent from including header files twice? The problem is I'm including the in MyClass.h and then I'm including MyClass.h in many files, so it includes multiple times and redefinition error occurs. How to prevent?
I'm using #pragma once instead of include guards, and I guess that's fine.
MyClass.h:
// MyClass.h
#pragma once
#include <winsock2.h>
class MyClass
{
// methods
public:
MyClass(unsigned short port);
virtual ~MyClass(void);
};
EDIT: Few of the errors I'm getting
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(460) : see previous definition of 'AF_IPX'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(124) : warning C4005: 'AF_MAX' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(479) : see previous definition of 'AF_MAX'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(485) : see declaration of 'sockaddr'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2143: syntax error : missing '}' before 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2143: syntax error : missing ';' before 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2059: syntax error : 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C2143: syntax error : missing ';' before '}'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(518) : warning C4005: 'IN_CLASSA' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(287) : see previous definition of 'IN_CLASSA'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(524) : warning C4005: 'IN_CLASSB' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(293) : see previous definition of 'IN_CLASSB'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(530) : warning C4005: 'IN_CLASSC' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(299) : see previous definition of 'IN_CLASSC'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(541) : warning C4005: 'INADDR_ANY' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(304) : see previous definition of 'INADDR_ANY'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(543) : warning C4005: 'INADDR_BROADCAST' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(306) : see previous definition of 'INADDR_BROADCAST'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(577) : error C2011: 'sockaddr_in' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(312) : see declaration of 'sockaddr_in'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(132) : error C2011: 'fd_set' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(68) : see declaration of 'fd_set'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(167) : warning C4005: 'FD_SET' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(102) : see previous definition of 'FD_SET'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(176) : error C2011: 'timeval' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(111) : see declaration of 'timeval'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(232) : error C2011: 'hostent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(167) : see declaration of 'hostent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(245) : error C2011: 'netent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(180) : see declaration of 'netent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(252) : error C2011: 'servent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(187) : see declaration of 'servent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(264) : error C2011: 'protoent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(199) : see declaration of 'protoent'
解决方案
This problem is caused when including <windows.h>
before <winsock2.h>
. Try arrange your include list that <windows.h>
is included after <winsock2.h>
or define _WINSOCKAPI_
first:
#define _WINSOCKAPI_ // stops windows.h including winsock.h
#include <windows.h>
// ...
#include "MyClass.h" // Which includes <winsock2.h>
See also this.
相关文章