无限的包含循环
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的描述完全正确,只是没有任何错误。在不同位置添加
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
#include
s to CPP files where possible这正是发生的情况。
这称为“包含防护”,用于避免无限递归包含。
That's exactly what happens.
This is called an "include guard" and is used to avoid infinitely recursive includes.