2012-04-15 3 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); 
} 

내가 '파일 1'전에 선언 'B'를 필요로 아직 동시에, 나는 그래서 내가 정의하는 방법을 모르는 나는 나에 대해 정의 될 필요가 .. '나'정의 시작 둘 다 전에 .. 나는 더 진보 된 해결책이 필요하다고 생각하지만 나는 모른다.

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

public: 
B(); 
} 

그래서 그 답을 찾을 수 있다면 다음 부분을 직접 확인해 볼 수 있습니다. 올바른 방향으로 가고 있는지 확인하는 것이 좋을 것이라고 생각되면 아래의 모든 코드를 붙여 넣습니다.

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

충분하지 않거나 읽지 않습니까? 여기에는 4 개의 파일 모두에 대한 주석을 제외한 모든 행이 있습니다. 인지 된 중요성 순서.

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

디버깅 태그가 제거되었습니다. 디버깅이라는 용어는 일반적으로 실행중인 프로그램에서 오류를 식별하고 수정하는 프로세스를 식별하는 데 사용되는 반면 컴파일 오류는 –

+0

입니다. 첫 번째 "오류"는 오류가 아니지만 다음 오류에 대한 정보입니다. 오류가 발생한 헤더 파일이 포함되어있다. (어떤 오류는'#include '이전에 어떤 결과가있을 수 있으므로 중요 할 수있다.) – celtschk

답변

5

B에 대한 참조와 포인터 만 사용하고 있으므로 처럼 앞으로 선언이 B이면 포함하는 대신 충분합니다.추가 :

namespace charon 
{ 
  class charon_ 
  { 

  }; 
} 

당신은 charon_이 클래스는 말할 수 없습니다 :

class B; // << Forward declaration of B 

class I 
{ 
private: 
B* my_private_b; 

public: 
I(B& handle); 
} 

File1.h

3

하나의 문제의 시작 부분에는이 선언이 아니라 정의이다 네임 스페이스 charon, 또한 비어 있다고 말합니다. 그러면 나중에 반원을 추가 할 수 없습니다.

더 좋은 방법은 클래스가 존재하는 컴파일러를 알려줍니다,하지만 세부 사항을 나뭇잎

namespace charon 
{ class charon_; } 

입니다.

+0

오오, 지금 당장 해보겠습니다. –

관련 문제