1. 程式人生 > >使用QT時,出現error C2011: 'sockaddr' : 'struct' type redefinition.

使用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’中又定義了它,所以會報這個錯誤咯。

以上拙見,如有錯誤,請毫不留情直接打臉,謝謝