这里我们有两个类,我们称之为Tree
和Fruit
。 A Tree
在任何给定时间只能有一个或没有Fruit
。 A Fruit
只能在一个Tree
上。从Tree
对象,您可以通过功能getTreeFruit()
获得其Fruit
。从Fruit
对象中,可以通过函数getFruitOwner()
获取其“所有者”,该函数返回Tree
对象。一个班级需要另一个班级,其他班级需要第一个班级。我怎么做?
现在在Tree
头,我们有这样的:
#include "Fruit.h"
class Tree {
private:
Fruit m_Fruit; // The fruit in the tree.
public:
Tree (Fruit tree_fruit);
Fruit getTreeFruit(); // Returns m_Fruit.
}
而且在Fruit
头:
#include "Tree.h"
class Fruit {
private:
Tree m_Owner; // The Tree object that "owns" the fruit.
public:
Fruit (Tree fruit_owner);
Tree getFruitOwner(); // Returns m_Owner.
}
我意识到Tree
和Fruit
包括对方的头文件,这会导致错误。我该如何着手解决这个错误?
非常感谢先进的。之类的:)
我做到了。有效。现在,我有另一种名为'drawFruit()'的方法,该方法采用'Fruit'所有者的'x'位置,并在某些数学运算后使用该值来定位水果。但是,我收到错误说'会员访问不完整类型'树'。那是什么意思?我必须在我的'Fruit.cpp'中包含'Tree.h'吗?顺便说一句,树的'x'值是公开的。 – alxcyl 2012-08-03 10:11:07
@LanceGray准确。 – 2012-08-03 10:12:31
我收到一个错误消息,说:“Tree :: Tree(std :: string const&)Tree.o中的树形结构i386的未定义符号: ”Fruit :: Fruit()“,引用来自: Tree :: Tree(int ,int,std :: string const&)在Tree.o ld:符号(s)not found for architecture i386' – alxcyl 2012-08-03 10:18:10