【问题标题】:Type Redefinition Error (using Header Guards)类型重定义错误(使用 Header Guards)
【发布时间】:2017-10-08 18:58:05
【问题描述】:

免责声明:C++ Redefinition Header Files (winsock2.h) 没有解决我的问题

在这个项目中,我尝试截取屏幕截图,然后使用 libjpeg-turbo 对其进行压缩。问题是我收到类似的错误

"sockaddr": "struct" Type redefinition

"nothl": Redefinition

ScreenWorker.h:

#pragma once
#ifndef SCREENWORKER_H
#define SCREENWORKER_H

#include <string>
#include <vector>
#include <thread>

#include <turbojpeg.h>

#include <Windows.h>

#include "..\API\NetClient.h"


class ScreenWorker {
private:
    NetClient* client;
public:
    int delay = 30;
    ScreenWorker(NetClient* client);
    HBITMAP GetScreenBmp(HDC hdc);
    void Update();
};
#endif

ScreenWorker.cpp:

#include "ScreenWorker.h"

ScreenWorker::ScreenWorker(NetClient* client) {
    this->client = client;
    Update();
}

HBITMAP ScreenWorker::GetScreenBmp(HDC hdc) {...}

void ScreenWorker::Update() {...}

main.cpp(DLL-Entry):

#pragma once
#include "..\..\Base\API\API\GladOSClient.h"
#include "ScreenWorker.h"

using namespace std;

BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, LPVOID Reserved) {
    return true;
}

NetClient.h(仅 Header 部分):

#pragma once
#ifndef NETCLIENT_H
#define NETCLIENT_H

#define _WINSOCKAPI_
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <vector>
#include <list>
#include <mutex>
#include <map>
#include <string>

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

#include "Utils.h"
#include "PacketHandler.h"
#include "Packet.h"

...

#endif

如您所见,我到处都在使用 header-guards,但是我还是遇到了这些错误。好像包含“Windows.h”有问题?

提前致谢!

编辑 我猜这个问题与 libjpegturbo 处理“Windows.h”的方式有关。目前我没有真正的方法来解决这个问题。也许我正在尝试将我需要的函数导出到一个单独的 DLL 中......希望这能解决它。

【问题讨论】:

标签: c++ redefinition


【解决方案1】:

经典问题。

只需从NetClient.h 中删除此行

#include <Windows.h>

包含&lt;Winsock2.h> 将为您引入&lt;windows.h&gt; 并纠正所有这些重新定义的问题。

【讨论】:

  • 感谢您的回答,但错误保持不变。我怀疑问题出在这一点上:#include #include 但我不知道为什么。
  • 删除所有对#include &lt;windows.h&gt;的引用并替换为#include &lt;winsock2.h&gt;
  • 包括ScreenWorker.h中的那个
  • 如果我替换ScreenWorker.h 中的include &lt;windows.h&gt;,它将找不到像BitBltCreateCompatibleBitmapGetDC、...之类的函数。我知道这没有任何意义,但就是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2014-10-02
  • 1970-01-01
相关资源
最近更新 更多