두 개체 (이중 연결된 자식 및 부모)에서 자식 및 부모 개체를 올바르게 참조하려면 어떻게해야합니까? 이 작업을 수행 할 때 컴파일 오류가 발생합니다 : **** 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;
}
이런! 감사. 그게 효과가 있었어. 그러나 Child의 인스턴스가 Parent에 있으면 포인터와 별도로이 Child 인스턴스에 액세스 할 수있는 방법이 있습니까? – jhtong
자녀는 부모의 회원이 될 수 있지만 순환 포함 문제에주의해야합니다. :) – billz