直接解决这个问题,在pro文件中添加如下代码
win32:DEFINES += _WINSOCKAPI_
更新:2017-1-6
之前其实没搞懂添加这个为什么就解决了。
这个问题出现的原因是,我们包含的别的头文件比如“windows.h”中包含了”winsock.h”。
但是
我们在写代码的时候,一般用
#include <WinSock2.h>
我截取了winsock.h头文件的一部分内容
#include <winapifamily.h>
/* WINSOCK.H--definitions to be used with the WINSOCK.DLL
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This header file corresponds to version 1.1 of the Windows Sockets specification.
*
* This file includes parts which are Copyright (c) 1982-1986 Regents
* of the University of California. All rights reserved. The
* Berkeley Software License Agreement specifies the terms and
* conditions for redistribution.
*
*/
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
#if _MSC_VER > 1000
#pragma once
#endif
//-----下面省略---------//
在这里,我们可以看出
#define _WINSOCKAPI_
的目的就是让别的头文件别包含了”winsock.h”内容。
如果没这么做,在”winsock.h”中我们定义了’sockaddr’,而你要用的‘WinSock2.h’中又定义了它,所以会报这个错误咯。
以上拙见,如有错误,请毫不留情直接打脸,谢谢
这个问题的详细解答与解决方法请查看
https://imron02.wordpress.com/2014/12/30/qt-struct-type-redefinitionredefinition-header/