开发者

C++ cyclic inclusion issue [duplicate]

This question already has answers here: Resolve build errors due to circular dependency amongst classes (12 answers) Closed 5 years ago.

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
  开发者_如何学JAVA   */
    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.


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.


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜