相互依赖的类声明

时间:2012-04-15 13:46:38

标签: c++ compiler-errors

我想弄清楚这个编译错误意味着什么,我希望我能解释清楚。

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

接下来将我带到上面的^文件,并以黄色加下划线:

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

类型标识符“charon_”(在头文件charon.h中定义的类)也以黄色加下划线可能是一个不同的错误。

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_()’

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

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

public:
I(B& handle);
}

不幸的是,我需要在“文件1”开始定义“我”之前声明“B”..但同时,我需要为B定义。所以我不知道如何在另一个之前定义..我猜我需要一个更先进的解决方案,但我不知道。

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

public:
B();
}

所以,如果我能找到答案,那么也许我可以自己查看下一部分。如果您认为检查我是否朝着正确的方向前进是一件好事,我会在下面粘贴所有相关代码。

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

不够长,没读过? 那么这里是除了注释之外的所有四个文件的代码。按照感知的重要性。

//////////////////////////////////////
/////
/////////////
/////
/////
/////
//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 */

这些是constructors / 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;
namespace chio
{
  chin_::chin_(charon::charon_& handle) {
    engine = handle;
  }
  chin_::~chin_() {
  }
}

对于结局,我个人讨厌阅读,所以我一直在推迟只是问这一切。所以如果你走到这一步,你可能也想要这个。

"/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

我很乐意回答我的简单版本。我已经没有想法来修复编译错误,我已经翻阅了我的巨大C ++参考资料,验证了我能想到的一切。所有语法看起来都是正确的,我认为只是我还没有专门学习如何操作编译器来做这样的事情。

我不知道,我现在可能只是在闲聊。至少在过去的3天里,这令我感到沮丧。

2 个答案:

答案 0 :(得分:5)

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

class B; // << Forward declaration of B

class I
{
private:
B* my_private_b;

public:
I(B& handle);
}

File1.h

的开头

答案 1 :(得分:4)

一个问题是,这不是声明,而是定义:

namespace charon
{
  class charon_
  {

  };
}

您不仅要说charon_是命名空间charon中的一个类,还要说它是空的。之后不允许您添加类成员。

更好的方法是

namespace charon
{ class charon_; }

它只是告诉编译器该类存在,但是没有详细说明。

相关问题