在 main 中访问类的模板友元函数。

发布于 2024-12-11 02:25:10 字数 1517 浏览 0 评论 0原文

我很难找到一个简单的解决方案。我正在使用以下类实现表达式树,我声明类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-12-18 02:25:10
template<class object> Tree<object>::expTree(Tree<object> *&T);  // ERROR
                     //^^^^^^^^^^^^^^ cause of the error

这个函数模板实际上是一个自由函数,并且将成为Tree类的友元。所以应该写成:

template<class object> expTree(Tree<object> *&T); 

即从声明中删除Tree::。我刚刚意识到它缺少返回类型,我想您的意思是 Tree是返回类型(而 Tree:: 是一个拼写错误在你的代码中)。如果是这样,请这样写:

template<class object> Tree<object> expTree(Tree<object> *&T); 

我想评论一下您命名模板参数和实参的风格。通常使用 TUV 等作为模板参数,而不是函数参数。因此,当您将声明写为:

 template<class T> Tree<T> expTree(Tree<T> *&object);  

嗯,我只是交换了名称。

template<class object> Tree<object>::expTree(Tree<object> *&T);  // ERROR
                     //^^^^^^^^^^^^^^ cause of the error

This function template is actually a free function, and will be a friend of the class Tree. So it should be written as:

template<class object> expTree(Tree<object> *&T); 

That is, remove Tree<object>:: from the declaration. I just realized that it is missing the return type, and I suppose you mean Tree<object> to be the return type (and Tree<object>:: is a typo in your code). If so, then write this:

template<class object> Tree<object> expTree(Tree<object> *&T); 

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:

 template<class T> Tree<T> expTree(Tree<T> *&object);  

Well I just swapped the names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文