本文介绍了C ++循环包含问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文件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

此文件event.hpp

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

好吧。在logger.hpp中我包含event.hpp,在event.hpp中我包含logger.hpp。

Well. In logger.hpp I include event.hpp and in event.hpp I include logger.hpp.


  • 我需要包含事件.hpp因为在logger.hpp中我需要定义运算符。

  • I need to include event.hpp because in logger.hpp I need to define the operator.

我需要包含logger.hpp,因为在event.hpp中,友谊是在类Event中定义。

I need to include logger.hpp because, in event.hpp, of the friendship to be defined in the class Event.

当然,这是循环递归

我试过这个:

1)在logger.hpp中:

1) In logger.hpp:

#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_

#include "event.hpp"

class Event; // Forward decl

// Class definitions
...

不起作用。编译器告诉我,在event.hpp中有一个名为Logger的无法识别的类型(当然他是对的):

Does not work. Compiler tells me that in event.hpp there is a not recognized type called Logger (and he is right of course):

编译器指示我在那里的行(在event.hpp中)友情声明。

Compiler indicates me the line (in event.hpp) where there is the friendship declaration.

2)在event.hpp中:

2) In event.hpp:

#ifndef _EVENT_HPP_
#define _EVENT_HPP_

#include <string>

#include "logger.hpp"

class Logger; // Forward decl

// Class definitions
...

不起作用。编译器告诉我,在logger.hpp中有一个名为Event的不可识别类型(并且,由于显而易见的原因,它是正确的):

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):

编译器指示我的行(在logger.hpp中)哪里有操作员声明。

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 :) ).

谢谢你。

推荐答案

logger.hpp 中摆脱 #includeevent.hpp - 如果您需要的只是对函数原型中的 Event 对象的引用,那么类Event 的前向声明应该足够了:

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
...

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

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

这篇关于C ++循环包含问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 11:12