Mutually Dependent class declarations

2019-02-26 02:11发布

问题:

I am trying to figure out what this compile error means, and I hope I explain this well.

In file included from sys/charon.cpp:4:0:

Following this takes me to that ^ file above, and underlines in yellow this:

#include "../headers/charon.h"

As well the type identifier "charon_", which is a class defined within the header file charon.h, is also underlined in yellow possibly a different error.

sys/../headers/charon.h:17:9: error: redefinition of ‘class charon::charon_’
sys/../headers/chio.h:86:9: error: previous definition of ‘class charon::charon_’
sys/charon.cpp:12:20: error: definition of implicitly-declared ‘charon::charon_::charon_()’

However I hope they are all related to the first error, I think it is related to something I am trying to do.

//File 1.h
/**********/
class I
{
private:
B* my_private_b;

public:
I(B& handle);
}

Unfortunately I need 'B' declared before 'file 1' begins defining 'I'.. yet at the same time, I need I to be defined for B. So I don't know how to define both before the other.. I am guessing I need a more advanced solution but I don't know.

//File 2.h
/***********/
class B
{
private:
I i;
O o;

public:
B();
}

So if I can find an answer to that, then maybe I could just check this next part for myself. If you think it would be good to check to see if I am going in the right direction I will paste all the code in question below.

.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.

Not Long Enough, Didn't Read? Well here are all the lines, except for the comments, of code from all four files. In order of perceived importance.

//////////////////////////////////////
/////
/////////////
/////
/////
/////
//File: chio.h

#ifndef CHIO_H
#define CHIO_H
#include <deque>
#include <queue>
#include <iostream>

using namespace std;

namespace charon
{
  class charon_
  {

  };
}
namespace chio
{ 
  class chout_
  {
  private:

  public:
    chout_();
    ~chout_();
  };

  class chin_
  {
  private:
    charon::charon_* engine;

  public:
    chin_(charon::charon_& handle);
    chin_();
    ~chin_();
  };
}

#endif  /* CHIO_H */

//////////////////////////////////////
/////
/////////////
/////
/////
/////
//File: charon.h/* 
//
 * File:   charon.h
 * Author: josh
 *
 * Created on 11 April 2012, 22:26
 */

#ifndef CHARON_H
#define CHARON_H
//#include "boost/thread.hpp"
#include "chio.h"

using namespace std;
using namespace chio;
namespace charon
{
  class charon_ {
  private:
    chout_ engine_output;
    chin_ engine_input;
    //boost::thread input_thread;
    //boost::thread output_thread;
    void start_threads();
    void stop_threads();

  public:
    charon_();
    charon_(charon_ &orig);
    ~charon_();
    void run();
  };
}


#endif  /* CHARON_H */

These are the constructors / cpp files.

charon.cpp
#include <iostream>
//#include <boost/thread.hpp>
//#include <boost/date_time.hpp>
#include "../headers/charon.h"

using namespace std;
using namespace charon;
using namespace chio;

namespace charon
{
  charon_::charon_(){
    engine_input = chio::chin_((charon_*)this);
  }
};

//^^charon.cpp^^
---------
//chin.cpp

#include <iostream>
#include <borland/conio.h>
#include <ncurses.h>
#include <deque>
#include "../headers/charon.h"

using namespace std;
using namespace charon;
using namespace chio;
namespace chio
{
  chin_::chin_(charon::charon_& handle) {
    engine = handle;
  }
  chin_::~chin_() {
  }
}

And for the finale, I personally hate reading so I've been putting off just asking all this of anybody. So if you have come this far you probably want this too.

"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/josh/Projects/Maze/Charon'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/charon
gmake[2]: Entering directory `/home/josh/Projects/Maze/Charon'
mkdir -p build/Debug/GNU-Linux-x86/sys
rm -f build/Debug/GNU-Linux-x86/sys/charon.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
In file included from sys/charon.cpp:4:0:
sys/../headers/charon.h:17:9: error: redefinition of ‘class charon::charon_’
sys/../headers/chio.h:86:9: error: previous definition of ‘class charon::charon_’
sys/charon.cpp:12:20: error: definition of implicitly-declared ‘charon::charon_::charon_()’
gmake[2]: *** [build/Debug/GNU-Linux-x86/sys/charon.o] Error 1
gmake[2]: Leaving directory `/home/josh/Projects/Maze/Charon'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/josh/Projects/Maze/Charon'
gmake: *** [.build-impl] Error 2

I would be simply glad to have an answer to my simpler version. I have run out of ideas to fix the compile errors and I have flipped through my huge C++ reference verifying everything I can think of. All the syntax looks to be correct, I think it is just that I have yet to specifically learn how to manipulate the compiler to do things like this.

I don't know, I am probably just rambling now. This has been frustrating me for the last 3 days at least.

回答1:

It seems like in File1.h a forward declaration of B would suffice instead of including it since you're only using references and pointers to B. Add:

class B; // << Forward declaration of B

class I
{
private:
B* my_private_b;

public:
I(B& handle);
}

at the beginning of File1.h



回答2:

One problem is that this isn't a declaration, but a definition:

namespace charon
{
  class charon_
  {

  };
}

You not only say that charon_ is a class in namespace charon, you also say that it is empty. You are then not allowed to add class members later.

A better way is

namespace charon
{ class charon_; }

which just tells the compiler that the class exists, but leaves out the details.