我有一个BinTree
描述二叉搜索树的类:
class BinTree
{
public:
BinTree();
~BinTree();
bool IsEmpty();
bool IsFull();
const int Size() const;
bool Add(Item data);
bool In(Item data);
bool Delete(Item data);
void Traverse();
...
}
对于 BinTree 类,我在 int main() 中创建了它的一个实例:
#include<iostream>
#include"BinTree.h"
#include"menu.h"
//extern BinTree mytree;
int main()
{
BinTree mytree;//экземпляр
return 0;
}
我需要这个实例也可以在另一个类中使用-menu
class menu
{
private:
static void upp(string str);
public:
static void addpet();
};
void menu::upp(string str)
{
for (int i = 0; i < str.length; i++)
{
str[i] = toupper(str[i]);
}
}
void menu::addpet()
{
Item temp;
cout << "Please enter name of pet: " << endl;
//std:sin >> temp.Name;
getline(std::cin, temp.Name);
cout << "Please enter pet kind: " << endl;
getline(std::cin, temp.Kind);
upp(temp.Name);
upp(temp.Kind);
mytree.Add(temp);
}
问题出现在这一行
mytree.Add(temp);
因为“id mytree 未定义”。那么如何让菜单类了解另一个类的实例呢?我想到的唯一一件事是在文件之间使用全局范围extern
(但这是一个极端情况)。有没有更好的方法,即不使用全局变量?
如果是这样?