如何在兩個對象(雙向鏈接的子對象和父對象)中正確引用子對象和父對象?當這樣做,我得到一個編譯錯誤:**** does not name a type
。由於#define標籤,我懷疑它與#include語句被忽略。這些標籤應該如何包含在內?如何在這兩個類中雙重引用子類和父類
的三個文件(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 2013-02-11 09:20:47
孩子可以是家長的成員,但你仍然需要小心通知包括問題:) – billz 2013-02-11 09:35:08