C++循环包含问题

发布于 2024-10-12 02:16:34 字数 1814 浏览 3 评论 0原文

我有这个文件 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 技术交流群。

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

发布评论

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

评论(2

茶花眉 2024-10-19 02:16:34

摆脱 logger.hpp 中的 #include "event.hpp" - 如果您需要的话,class Event 的前向声明就足够了是对函数原型中的 Event 对象的引用:

#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_

// #include "event.hpp"  // <<-- get rid of this line

class Event; // Forward decl

// Class definitions
...

logger.cppclass Logger 的实现可能需要包含 事件.hpp。

Get rid of the #include "event.hpp" in logger.hpp - the forward declaration of class Event should be enough if all you need is a reference to an Event object in the function prototype:

#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_

// #include "event.hpp"  // <<-- get rid of this line

class Event; // Forward decl

// Class definitions
...

The implementation of class Logger in logger.cpp will likely need to include event.hpp.

话少心凉 2024-10-19 02:16:34

当您转发声明时,不要放入#include。就像

class Event;
class Logger {
public:
    /*!
     * Constructor
     */
    Logger();
    /*!
     * Destructor
     */
    ~Logger();
    /*!
     * My operator
     */
    Logger& operator<<(const Event& e);
private:
    ...
};

没有#include "event.hpp"一样

When you forward declare, don't put in the #include. Do it like

class Event;
class Logger {
public:
    /*!
     * Constructor
     */
    Logger();
    /*!
     * Destructor
     */
    ~Logger();
    /*!
     * My operator
     */
    Logger& operator<<(const Event& e);
private:
    ...
};

without the #include "event.hpp"

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