简体   繁体   English

在main中访问类的模板朋友功能。

[英]Accessing template friend function of class in main.

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. 我正在使用以下类实现表达式树,我声明了Tree类的好友函数。 My problem comes when I try to get it started in main. 当我尝试开始使用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." 总的来说,我得到“错误:未在此范围内声明expTree”。 I also get, " error: expected constructor, destructor, or type conversion before â;â token" on the second line of this code.. Anyone have any pointers? 在此代码的第二行上,我还会得到“错误:预期的构造函数,析构函数或类型转换,然后在</ Token>之前。”任何人都有指针吗?

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 . 该函数模板实际上是一个自由函数,它将成为Tree类的朋友。 So it should be written as: 因此,应将其写为:

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

That is, remove Tree<object>:: from the declaration. 也就是说,从声明中删除Tree<object>:: 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). 我只是意识到它缺少返回类型,我想你是说Tree<object>是返回类型(而Tree<object>::是代码中的错字)。 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. 通常将TUV等用于模板参数,而不用于函数参数。 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. 好吧,我只是交换了名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM