2012-04-15 107 views
2

我想弄清楚這個編譯錯誤是什麼意思,我希望我解釋得很好。相互依賴的類聲明

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

在此之後帶我到^文件的上方,並且在黃色此強調:

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

同樣類型標識符「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); 
} 

不幸的是,我需要「B」之前「文件1」聲明開始定義「我」 ..但在同一時間,我需要我爲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 */ 

這些是構造函數/ 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天裏,這令我感到沮喪。

+0

刪除了調試標籤。術語「調試」通常用於識別和糾正正在運行的程序中的錯誤,而這是一個編譯錯誤 – 2012-04-15 13:56:15

+0

第一個「錯誤」不是錯誤,而是關於錯誤的信息:它告訴包含錯誤發生頭文件的位置(這可能很重要,因爲發現的錯誤可能是'#include'之前的結果)。 – celtschk 2012-04-15 14:56:55

回答

5

好像在File1.hB預先聲明就足夠了,而不是因爲你只使用到B引用和指針,包括它。地址:

class B; // << Forward declaration of B 

class I 
{ 
private: 
B* my_private_b; 

public: 
I(B& handle); 
} 

File1.h

3

一個問題的開始是,這是不是一個宣言,而是一個定義:

namespace charon 
{ 
  class charon_ 
  { 

  }; 
} 

你不僅說charon_屬於一類命名空間charon,你也說它是空的。之後您不能再添加班級成員。

更好的方法是

namespace charon 
{ class charon_; } 

這只是告訴了類存在的編譯器,但保留了細節。

+0

哦,我現在就試試。 – 2012-04-15 14:16:07