C++由于多次包含头文件而重新定义

发布于 2024-08-03 22:53:32 字数 1326 浏览 7 评论 0原文

正如标题所说。由于多次包含头文件,我遇到了重新定义错误。我知道是这个原因,但不知道如何解决。是的,我之前提前一个小时在 SO 中发布了同样的问题。但我无法正确解释(我认为是这样)并且没有得到预期的答案。这是链接:

C++ 重定义头文件

我不会编辑该问题,因为它已经已满:)。

好吧,我有一些类,它们的结构如下:

ma​​in.cpp:

#include "Server.h"
#include "Handler.h"
#include "Processor.h"

int main(int argc, char* argv[])
{

}

Server.h:

// Server.h
#pragma once

#include <winsock2.h>

Handler.h:

// Handler.h
#pragma once

#include <string>
#include <vector>

#include "Server.h"

Processor.cpp:

// Processor.cpp

#include "StdAfx.h"
#include "Processor.h"
#include "Handler.h"

Server.cpp:

// Server.cpp

#include "Server.h"
#include "Processor.h"

问题是 被多次包含,不知道在哪里,但它在哪里。 #pragma Once 与我的编译器(本例中为 MSVC2008)具有相同的用途

#ifndef SOME_FILE_H
#define SOME_FILE_H
// code here
#endif // SOME_FILE_H

。所以我非常确定我不需要标头包含防护装置。但是你能发现我在哪里犯了 被包含两次的错误吗?我该如何解决?

谢谢

As, the title says. I'm encountering redefinition errors due to including header files multiple times. I know its because of that, but I don't know how to resolve. Yes, I previously posted the same problem in SO an hour ahead. But I wasn't able to explain properly (I think so) and didn't get answers expected. Here is the link:

C++ Redefinition Header Files

I'm not editing that question since it has been filled up :).

Okay I have some classes and the structure of them is like this:

main.cpp:

#include "Server.h"
#include "Handler.h"
#include "Processor.h"

int main(int argc, char* argv[])
{

}

Server.h:

// Server.h
#pragma once

#include <winsock2.h>

Handler.h:

// Handler.h
#pragma once

#include <string>
#include <vector>

#include "Server.h"

Processor.cpp:

// Processor.cpp

#include "StdAfx.h"
#include "Processor.h"
#include "Handler.h"

Server.cpp:

// Server.cpp

#include "Server.h"
#include "Processor.h"

The problem is that <winsock2.h> is included multiple times, don't know where but it is. #pragma once serves the same purpose as

#ifndef SOME_FILE_H
#define SOME_FILE_H
// code here
#endif // SOME_FILE_H

in my compiler (MSVC2008 in this case). So I'm pretty much sure I don't need the header include guards. But can you spot where I'm doing the mistake by which <winsock2.> is included twice and how may I resolve?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

素罗衫 2024-08-10 22:53:32

在您的项目设置中:
项目属性->配置->高级->显示包括。

它将转储标头包含树,从那里您将能够看到罪魁祸首。

In your project settings:
Project properties -> configuration -> advanced -> show includes.

It will dump the header include tree, from there you'll be able to see the culprit.

江南月 2024-08-10 22:53:32

我最近遇到了同样的问题,并在包含 windows.h 之前解决了它,包括 winsock2.h

I had the same problem recently and solved it including winsock2.h before including windows.h.

萌无敌 2024-08-10 22:53:32

在包含 stdafx 或 windows 之前,您需要部分或全部这些。

#define _MSWSOCK_
#define NCB_INCLUDED
#define _WINSOCK2API_
#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */

You need some or all of these before you include stdafx or windows.

#define _MSWSOCK_
#define NCB_INCLUDED
#define _WINSOCK2API_
#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
殤城〤 2024-08-10 22:53:32

尝试替换

#include <winsock2.h>

#ifndef _WINSOCK2API_
#include <winsock2.h>
#endif

由于_WINSOCK2API_是在winsock2.h中定义的,编译器不会尝试多次包含它。

try replacing

#include <winsock2.h>

with

#ifndef _WINSOCK2API_
#include <winsock2.h>
#endif

Since _WINSOCK2API_ is defined inside winsock2.h, compiler will not try to include it multiple times.

以歌曲疗慰 2024-08-10 22:53:32

您是否尝试过我们在其他答案中提出的建议?

说真的,尝试使用包含防护而不是#pragma Once

如果您仍然遇到问题,那么也许可以回来并在 SO 上发布另一个问题。不要因为您不愿意(或无法)接受我们的建议而针对同一件事发布多个问题!

Have you tried any of the suggestions we made in your other answer?

Seriously, try using include guards instead of #pragma once.

If you still get the problem, then maybe come back and post another question on SO. Don't post multiple questions about the same thing because you're unwilling (or unable) to accept our advice!

神经大条 2024-08-10 22:53:32

我记得好像有这个问题。
我认为windows.h和winsock2.h之间存在依赖性问题
我似乎记得我通过在使用 windows.h 之前总是在 Winsock2.h 之前包含 windows.h 来解决这个问题。

I seem to remember having this problem.
I think there is a dependency issue between windows.h and winsock2.h
I seem to remember I got around it by always including windows.h before winsock2.h wherever it was used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文