C++ cyclic inclusion issue [duplicate]

2019-02-19 01:16发布

This question already has an answer here:

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.

2条回答
贪生不怕死
2楼-- · 2019-02-19 01:20

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"

查看更多
倾城 Initia
3楼-- · 2019-02-19 01:26

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.

查看更多
登录 后发表回答