2013-02-11 3 views
0

두 개체 (이중 연결된 자식 및 부모)에서 자식 및 부모 개체를 올바르게 참조하려면 어떻게해야합니까? 이 작업을 수행 할 때 컴파일 오류가 발생합니다 : **** does not name a type. #include 문이 #define 태그로 인해 생략되는 것과 관련이 있다고 생각합니다. 따라서 이러한 태그는 어떻게 포함되어야합니까? 같은 작성두 클래스 내에서 자식 및 부모 클래스를 이중 참조하는 방법

세 개의 파일 (Parent.h, Child.h, MAIN.CPP는) :

/* Parent.h */ 
#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

#include "Child.h" 

class Parent { 
public: 
    Parent() {} 
    ~Parent() {} 
    void do_parent(Child* arg); 
}; 
#endif 

/* Child.h */ 
#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

#include "Parent.h" 

class Child { 
public: 
    Child() {} 
    ~Child() {} 
    void do_child(Parent* arg); 
}; 
#endif 

/* main.cpp */ 
#include "child.h" 
#include "parent.h" 

int main() 
{ 
    Child a(); 
    Parent b(); 
    a.do_parent(Child& arg); 
    return 0; 
} 

답변

1

당신은 두 가지 기능을 정의

아이 : 대신 개체로, most vexing parse

업데이트

Child a();    // a is a function returns Child object 
Parent b();    // b is a function returns Parent object 
a.do_parent(Child& arg); 

또한

Child a;   // a is a Child object 
Parent b;   // b is a Parent object 
b.do_parent(&a); 

하려면 원형이 포함 깰 circular include 문제를 가지고 볼 수, 당신은 하나 개의 유형을 전달합니다 .h

#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

//#include "Parent.h" Don't include Parent.h 
class Parent;   // forward declaration 
class Child { 
public: 
    Child() {} 
    ~Child() {} 
    void do_child(Parent* arg); 
}; 
#endif 

Child.cpp

#include "Parent.h" 
// blah 
+0

이런! 감사. 그게 효과가 있었어. 그러나 Child의 인스턴스가 Parent에 있으면 포인터와 별도로이 Child 인스턴스에 액세스 할 수있는 방법이 있습니까? – jhtong

+0

자녀는 부모의 회원이 될 수 있지만 순환 포함 문제에주의해야합니다. :) – billz

1

당신은 헤더 파일의 순환 종속성이 있습니다. 머리글 중 하나에서 클래스 중 하나만 선언하면됩니다.

/* Parent.h */ 
#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

//#include "Child.h" ----> Remove it 
class Child;   //Forward declare it 

class Parent { 
public: 
    Parent() {} 
    ~Parent() {} 
    void do_parent(Child* arg); 
}; 
#endif 
+0

감사합니다. 어떻게 완료해야합니까?Child 헤더에 #include "Parent.h"를 선언하고 Parent 헤더에 #include "Child.h"를 선언합니까? – jhtong

+0

@toiletfreak : 이미 코드 작성 방법을 보여 줬습니다. –

+0

Parent.h에 #include 을 넣으려고했지만'does not a type'이라는 오류가 발생합니다. – jhtong

1

를 사용하여 클래스의 프로토 타입/앞으로 선언 :

class Child; 

class Parent; 

서로의 클래스 선언 이전과는 포함 제거 예를 들어.

a.do_parent(Child& arg); 

당신은을 통과해야합니다

0

오류, 내가이 (편집/링커 오류에 대해 물어 때 항상 완벽한편집되지 않은 오류 메시지를 포함한다) 생각 ,이 라인 여기 Child 객체에 대한 포인터가 아닌 변수 선언 :

b.do_parent(&a); 
0

당신은 앞으로 선언 class Parent 또는 class Child이 필요합니다. 파일 중 하나를 수정하십시오.

는 Parent.h 당신에 :

#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

class Child;  // Forward declaration of class Child 

class Parent { 
public: 
    Parent() {} 
    ~Parent() {} 
    void do_parent(Child* arg); 
}; 
#endif 

아니면 Child.h에 :

#pragma once 
#ifndef _CHILD_CLS 
#define _CHILD_CLS 

class Parent; // Forward declaration of class Parent 

class Child { 
public: 
    Child() {} 
    ~Child() {} 
    void do_child(Parent* arg); 
}; 
#endif 

This question 당신에게 많은 도움이됩니다.

관련 문제