无限的包含循环

发布于 2024-12-12 21:38:23 字数 1516 浏览 2 评论 0原文

可能的重复:
C 头文件循环

原始问题:

我总是无法理解为什么以下给出错误:

something.h

#ifndef SOMETHING_H
#define SOMETHING_H

#include "somethingelse.h"

#endif

somethingelse.h

#ifndef SOMETHINGELSE_H
#define SOMETHINGELSE_H

#include "something.h"

#endif

为什么这会给出错误?

1) SOMETHING_H 未定义

2) SOMETHING_H 已定义,someelse.h 被包含

3) SOMETHINGELSE_H 未定义,已定义,并且 Something.h 被包含

4) SOMETHING_H 已定义,跳转到 #endif,这应该是结束它?


编辑:

事实证明它根本没有给出任何错误。然而,以下内容是这样的:

something.h

#pragma once

#include "somethingelse.h"

class something {
    int y;
    somethingelse b;
};

somethingelse.h

#pragma once

#include "something.h"

class somethingelse {
    something b;
    int x;
};

这是合乎逻辑的,因为当“somethingelse”需要该类的实例时,类“something”尚未定义。

该问题通过前向定义解决:

在.cpp中的something.h

#pragma once

class somethingelse;

class something {
    int y;
    somethingelse* pB; //note the pointer. Correct me if I'm wrong but I think it cannot be an object because the class is only declared, not defined.
};

,您可以包含“somethingelse.h”,并创建该类的实例。

Possible Duplicate:
C header file loops

Original Question:

I always had problems understanding why the following gives errors:

something.h

#ifndef SOMETHING_H
#define SOMETHING_H

#include "somethingelse.h"

#endif

somethingelse.h

#ifndef SOMETHINGELSE_H
#define SOMETHINGELSE_H

#include "something.h"

#endif

Why does this give errors?

1) SOMETHING_H is not defined

2) SOMETHING_H becomes defined, somethingelse.h get included

3) SOMETHINGELSE_H is not defined, becomes defined, and something.h gets included

4) SOMETHING_H is defined, jump to #endif, this should be the end of it?


EDIT:

turns out it doesn't give any errors at all. However the following does:

something.h

#pragma once

#include "somethingelse.h"

class something {
    int y;
    somethingelse b;
};

somethingelse.h

#pragma once

#include "something.h"

class somethingelse {
    something b;
    int x;
};

And it is logical, because the class 'something' is not yet defined when 'somethingelse' needs an instance of that class.

The problem is solved by forward definition:

something.h

#pragma once

class somethingelse;

class something {
    int y;
    somethingelse* pB; //note the pointer. Correct me if I'm wrong but I think it cannot be an object because the class is only declared, not defined.
};

in the .cpp, you can include "somethingelse.h", and make instances of the class.

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

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

发布评论

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

评论(2

惜醉颜 2024-12-19 21:38:23

你的描述完全正确,只是没有任何错误。在不同位置添加pragma message("Some text")(假设为 Visual Studio)以跟踪流程。

正如其他发帖者已经指出的那样,您的头文件很可能包含相互需要彼此定义的类,这就是问题的原因。此类问题通常通过

  • 尽可能使用前向引用
  • 来解决尽可能将 #include 移动到 CPP 文件

Your description is exactly correct, except that there is no error at all. Add pragma message("Some text") (assuming Visual Studio) at various places to trace the flow.

As other posters have already noted, your header files most likely contain classes that mutually require each other's definition, and that is the cause of the problem. This sort of problem is usually solved by

  • Using forward references where possible
  • Moving #includes to CPP files where possible
那片花海 2024-12-19 21:38:23

这正是发生的情况。

这称为“包含防护”,用于避免无限递归包含。

That's exactly what happens.

This is called an "include guard" and is used to avoid infinitely recursive includes.

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