使用QT时,出现error C2011: 'sockaddr' : 'struct' type redefinition.

直接解决这个问题,在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/

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值