C++循环包含问题
我有这个文件 logger.hpp:
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include "event.hpp"
// Class definitions
class Logger {
public:
/*!
* Constructor
*/
Logger();
/*!
* Destructor
*/
~Logger();
/*!
* My operator
*/
Logger& operator<<(const Event& e);
private:
...
};
#endif
还有这个文件 event.hpp
#ifndef _EVENT_HPP_
#define _EVENT_HPP_
#include <string>
#include "logger.hpp"
// Class definitions
class Event {
public:
/*!
* Constructor
*/
Event();
/*!
* Destructor
*/
~Event();
/* Friendship */
friend Logger& Logger::operator<<(const Event& e);
};
#endif
好吧。在 logger.hpp 中,我包含 event.hpp,在 event.hpp 中,我包含 logger.hpp。
我需要包含 event.hpp 因为在 logger.hpp 中我需要定义运算符。
我需要包含 logger.hpp,因为在 event.hpp 中,要在类 Event 中定义友谊。
嗯,这当然是一个循环递归。
我尝试过:
1)在 logger.hpp 中:
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include "event.hpp"
class Event; // Forward decl
// Class definitions
...
不起作用。编译器告诉我,在 event.hpp 中有一个无法识别的类型,称为 Logger(当然他是对的):
ISO C++ 禁止声明 没有类型的“记录器”
编译器的“记录器”向我指示了有友谊声明的行(在 event.hpp 中)。
2)在event.hpp中:
#ifndef _EVENT_HPP_
#define _EVENT_HPP_
#include <string>
#include "logger.hpp"
class Logger; // Forward decl
// Class definitions
...
不起作用。编译器告诉我,在 logger.hpp 中,有一个名为 Event 的无法识别的类型(并且,由于显而易见的原因,它是正确的):
ISO C++ 禁止声明“Event” 没有类型
编译器向我指示存在运算符声明的行(在 logger.hpp 中)。
嗯……不知道如何面对?我尝试了一切,我到处提出声明,但是,当然,它们没有任何帮助。 怎么解决这个问题??? (我认为存在最佳实践,希望如此:))。
谢谢。
I have this file logger.hpp:
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include "event.hpp"
// Class definitions
class Logger {
public:
/*!
* Constructor
*/
Logger();
/*!
* Destructor
*/
~Logger();
/*!
* My operator
*/
Logger& operator<<(const Event& e);
private:
...
};
#endif
And this file event.hpp
#ifndef _EVENT_HPP_
#define _EVENT_HPP_
#include <string>
#include "logger.hpp"
// Class definitions
class Event {
public:
/*!
* Constructor
*/
Event();
/*!
* Destructor
*/
~Event();
/* Friendship */
friend Logger& Logger::operator<<(const Event& e);
};
#endif
Well. In logger.hpp I include event.hpp and in event.hpp I include logger.hpp.
I need to include event.hpp because in logger.hpp I need to define the operator.
I need to include logger.hpp because, in event.hpp, of the friendship to be defined in the class Event.
Well this is, of course, a cyclic recursion.
I tried this:
1) In logger.hpp:
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include "event.hpp"
class Event; // Forward decl
// Class definitions
...
Does not work. Compiler tells me that in event.hpp there is a not recognized type called Logger (and he is right of course):
ISO C++ forbids declaration of
‘Logger’ with no type
Compiler indicates me the line (in event.hpp) where there is the friendship declaration.
2) In event.hpp:
#ifndef _EVENT_HPP_
#define _EVENT_HPP_
#include <string>
#include "logger.hpp"
class Logger; // Forward decl
// Class definitions
...
Does not work. Compiler tells me that in logger.hpp there is a not recognized type called Event (and, again, it is right for obvious reasons):
ISO C++ forbids declaration of ‘Event’
with no type
Compiler indicates me the line (in logger.hpp) where there is the operator declaration.
Well... do not know how to face this? I tried everything, I put forward declarations everywhere, but, of course, they are not of any help.
How to solve this??? (I suppose a best practice exists, better I hope so :) ).
Thankyou.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
摆脱
logger.hpp
中的#include "event.hpp"
- 如果您需要的话,class Event
的前向声明就足够了是对函数原型中的Event
对象的引用:logger.cpp
中class Logger
的实现可能需要包含事件.hpp。
Get rid of the
#include "event.hpp"
inlogger.hpp
- the forward declaration ofclass Event
should be enough if all you need is a reference to anEvent
object in the function prototype:The implementation of
class Logger
inlogger.cpp
will likely need to includeevent.hpp
.当您转发声明时,不要放入
#include
。就像没有
#include "event.hpp"
一样When you forward declare, don't put in the
#include
. Do it likewithout the
#include "event.hpp"