2

我试图弄清楚这个编译错误是什么意思,我希望我能很好地解释这一点。

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”开始定义“I”之前声明“B”。但同时,我需要为 B 定义 I。所以我不知道如何在另一个之前定义两者。我我猜我需要一个更高级的解决方案,但我不知道。

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

这些是构造函数/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 天里,这让我很沮丧。

4

2 回答 2

5

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

class B; // << Forward declaration of B

class I
{
private:
B* my_private_b;

public:
I(B& handle);
}

在。。。之初File1.h

于 2012-04-15T13:51:30.637 回答
3

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

namespace charon
{
  class charon_
  {

  };
}

您不仅说这charon_是 namespace 中的类charon,还说它是空的。然后,您以后就不能再添加班级成员了。

更好的方法是

namespace charon
{ class charon_; }

它只是告诉编译器该类存在,但忽略了细节。

于 2012-04-15T14:14:55.953 回答