在 main 中访问类的模板友元函数。
我很难找到一个简单的解决方案。我正在使用以下类实现表达式树,我声明类 Tree 的友元函数。当我尝试在 main 中启动它时,我的问题就出现了。
template<class object> class Tree;
template<class object> Tree<object>::expTree(Tree<object> *&T); // ERROR
template<class object>
struct Node
{
object info;
Node *next;
Node<object>():info(0), next(NULL) {}
Node<object>(const object &element, Node *n = NULL):
info(element), next(n){}
};
template<class object>
class Stack
{
public:
Stack();
~Stack();
void makestackempty();
bool stackEmpty() const;
void push(object &item);
void pop (object &item);
void printStack() const;
private:
Node<object> *top;
};
template<class object>
struct TreeNode
{
object info;
TreeNode *right;
TreeNode *left;
TreeNode()
{}
};
template<class object>
class Tree
{
private:
TreeNode<object> *root;
public:
Tree();
~Tree();
Tree(const Tree<object> &rhs); // copy
void operator=(const Tree<object> &rhs);
void copyconst(TreeNode<object> *orig, TreeNode<object> *&rhs);
void makeEmpty(TreeNode<object> *&tree);
bool isEmpty()const;
friend Tree<object> expTree(Tree<object> *&T){
buildTree(T.root);
};
void buildTree(TreeNode<object> *&tree);
void printTree(TreeNode<object> *&tree)const;
};
在 main 中,我收到“错误:expTree 未在此范围内声明”。 我还得到“错误:预期的构造函数、析构函数或类型转换在 â;â 标记之前” 在此代码的第二行.. 有人有任何指点吗?
I am having a hard time finding a simple solution to this. I am implementing an expression tree using the following classes, I declare a friend function of class Tree. My problem comes when I try to get it started in main.
template<class object> class Tree;
template<class object> Tree<object>::expTree(Tree<object> *&T); // ERROR
template<class object>
struct Node
{
object info;
Node *next;
Node<object>():info(0), next(NULL) {}
Node<object>(const object &element, Node *n = NULL):
info(element), next(n){}
};
template<class object>
class Stack
{
public:
Stack();
~Stack();
void makestackempty();
bool stackEmpty() const;
void push(object &item);
void pop (object &item);
void printStack() const;
private:
Node<object> *top;
};
template<class object>
struct TreeNode
{
object info;
TreeNode *right;
TreeNode *left;
TreeNode()
{}
};
template<class object>
class Tree
{
private:
TreeNode<object> *root;
public:
Tree();
~Tree();
Tree(const Tree<object> &rhs); // copy
void operator=(const Tree<object> &rhs);
void copyconst(TreeNode<object> *orig, TreeNode<object> *&rhs);
void makeEmpty(TreeNode<object> *&tree);
bool isEmpty()const;
friend Tree<object> expTree(Tree<object> *&T){
buildTree(T.root);
};
void buildTree(TreeNode<object> *&tree);
void printTree(TreeNode<object> *&tree)const;
};
In main, I get "error: expTree was not declared in this scope."
I also get, " error: expected constructor, destructor, or type conversion before â;â token"
on the second line of this code..
Anyone have any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个函数模板实际上是一个自由函数,并且将成为
Tree
类的友元。所以应该写成:即从声明中删除
Tree
我想评论一下您命名模板参数和实参的风格。通常使用
T
、U
、V
等作为模板参数,而不是函数参数。因此,当您将声明写为:嗯,我只是交换了名称。
This function template is actually a free function, and will be a friend of the class
Tree
. So it should be written as:That is, remove
Tree<object>::
from the declaration. I just realized that it is missing the return type, and I suppose you meanTree<object>
to be the return type (andTree<object>::
is a typo in your code). If so, then write this:I would like to comment on your style of naming the template parameters and arguments. It is customary to use
T
,U
,V
etc for template parameters, not for function argument. So it feels good when you write the declaration as:Well I just swapped the names.