相关疑难解决方法(0)

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
Run Code Online (Sandbox Code Playgroud)

而这个文件是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
Run Code Online (Sandbox Code Playgroud)

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

  • 我需要包含event.hpp,因为在logger.hpp中我需要定义运算符.

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

当然,这是一个循环递归.

我试过这个: …

c++ include-guards include header-files cyclic-reference

3
推荐指数
1
解决办法
6160
查看次数