相互依赖的类声明 [英] Mutually Dependent class declarations

查看:200
本文介绍了相互依赖的类声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 在文件中包含了这个编译错误的意思,我希望我能解释一下。从sys / charon.cpp:4:0:

并以黄色下划线:

  #include../headers/charon.h

同样,在头文件charon.h中定义的类型标识符charon_不同的错误。

  sys /../ headers / charon.h:17:9:error:redefinition' :charon_'
sys /../ headers / chio.h:86:9:error:以前的'class charon :: charon_'定义
sys / charon.cpp:12:20:定义的隐式声明的'charon :: charon _ :: charon_()'

但是我希望他们所有相关的第一个错误,我认为这是与我想做的事情有关。

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

public:
I(B& handle);不幸的是,我需要'B'之前'文件1'开始定义'I' '..但同时,我需要我定义为B.所以我不知道如何定义两个之前的其他..我猜我需要一个更先进的解决方案,但我不知道。 / p>

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

public:
B();
}

所以如果我能找到答案,这下一部分为我自己。如果你认为检查我是否正确的方向,我会粘贴所有的代码在下面。



。#。#。# 。#。#。#。#。#。#。#。#。#。#。#。#。#。#。#。#。#。#。。#。#。#。#。



不够长,没有阅读吗?
这里是所有的行,除了注释,来自所有四个文件的代码。按感知的重要性顺序。

  //////////////////// //////////////////// 
/////
/////////////
// ///
/////
/////
//文件:chio.h

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

using namespace std;

namespace charon
{
class charon_
{

};
}
命名空间chio
{
class chout_
{
private:

public:
chout_ ;
〜chout_();
};

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

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

#endif / * CHIO_H * /

////////////////////// ////////////////
/////
/////////////
//// /
/////
/////
//文件:charon.h / *
//
*文件:charon.h
*作者:josh
*
*创建日期:2012年4月11日,22:26
* /

#ifndef CHARON_H
#define CHARON_H
//#includeboost / thread.hpp
#includechio.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 * /

这些是构造函数/ cpp文件。

  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;
命名空间chio
{
chin _ :: chin_(charon :: charon_& handle){
engine = handle;
}
chin _ ::〜chin_(){
}
}


b $ b

对于结局,我个人讨厌阅读,所以我一直在推迟只是问这一切的任何人。因此,如果你已经到这里,你可能也想要这个太。

 / usr / bin / gmake-f nbproject / Makefile -Debug.mk QMAKE = SUBPROJECTS = .build-conf 
gmake [1]:输入目录`/ home / josh / Projects / Maze / Charon'
/ usr / bin / gmake /Makefile-Debug.mk dist / Debug / GNU-Linux-x86 / charon
gmake [2]:输入目录`/ home / josh / Projects / Maze / Charon'
mkdir -p build / Debug / GNU-Linux-x86 / sys
rm -f build / Debug / GNU-Linux-x86 / sys / charon.od
g ++ -c -g -MMD -MP -MF build / Debug / GNU -Linux-x86 / sys / charon.od -o build / Debug / GNU-Linux-x86 / sys / charon.o sys / charon.cpp
从sys / charon.cpp:4:
sys /../ headers / charon.h:17:9:error:redefinition'class charon :: charon_'
sys /../ headers / chio.h:86:9:error :前面定义的'charon :: charon_'
sys / charon.cpp:12:20:error:定义隐式声明的'charon :: charon _ :: charon_()'
gmake [2 ]:*** [build / Debug / GNU-Linux-x86 / sys / charon.o]错误1
gmake [2]:离开目录`/ home / josh / Projects / Maze / Charon'
gmake [1]:*** [.build-conf]错误2
gmake [1]:离开目录`/ home / josh / Projects / Maze / Charon'
gmake:*** [ .build-impl]错误2



我会很高兴有一个更简单的版本的答案。我已经用尽了想法来修复编译错误,我已经翻转了我的巨大的C ++参考,验证我可以想到的一切。所有的语法看起来是正确的,我认为这是我还没有专门学习如何操作编译器来做这样的事情。



我不知道,我现在可能只是漫步。

解决方案

它似乎在 File1。 h B 的向前声明就足够了,而不是包含它,因为你只使用引用和指针 B 。添加:

  B类; //<< B的转发声明

class I
{
private:
B * my_private_b;

public:
I(B& handle);
}

开头 File1.h


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.

解决方案

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

这篇关于相互依赖的类声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆