'depth' 的外联定义与 'Tree<E>' 中的任何声明都不匹配(2 个声明错误)

Out-of-line definition of 'depth' does not match any declaration in 'Tree<E>' (2 declaration errors)

这是一个 General Trees 程序,我试图在其中找到树的深度(深度为 2)并计算目录的大小。这两个错误是声明错误(接近 Tree.cpp 的底部)并且似乎无法正确声明函数。

Tree.cpp

#include "Tree.h"
#include <iostream>
using namespace std;

template <typename E>
bool Tree<E>::Position::isRoot() const
{
   if (u->parent == NULL)
       return true;
else
    return false;
}

template <typename E>
bool Tree<E>::Position::isExternal() const
{
    return (u->child.empty());

}

template <typename E>
bool Tree<E>::Position::operator==(Tree<E>::Position p)
{
    if (u == p.u)
        return true;
    else
        return false;
}

template <typename E>
Tree<E>::Position::Position(TreeNode *v)
{
    u = v;
}

template <typename E>
typename Tree<E>::Position Tree<E>::Position::parent()
{
    return Tree<E>::Position(u->parent);

}

template <typename E>
typename Tree<E>::PositionList Tree<E>::Position::children()
{
    return (u->child);

}

template <typename E>
E Tree<E>::Position::operator*()
{
    return u->e;
}

template <typename E>
typename Tree<E>::Position Tree<E>::root() 
{
    return typename Tree<E>::Position(r);
}

template <typename E>
int Tree<E>::size() const
{
    return this->n;
}

template <typename E>
bool Tree<E>::empty() const
{
    return size() == 0;
}

template <typename E>
void Tree<E>::insert(E e, Tree<E>::Position p)
{
    TreeNode *v = new TreeNode() ;
    v->e = e;
    if (empty())
        v->parent = NULL;
    else
        v->parent = p.u;
    if (!empty() )
    {
        p.u->child.insertBack(v);
    }
    else
    {
        v->parent = NULL;
        r = v;
    }
    ++n;
}

template <typename E>
NodeList<typename Tree<E>::Position>  Tree<E>::Positions()
{
    typename Tree<E>::PositionList list;
    preorder(r, list);
    return list;
}

template <typename E>
void Tree<E>::preorder(Tree<E>::Position  p, Tree<E>::PositionList &pl)
{
    pl.insertBack(p);
    if (p.u->child.empty()==false)
        for(typename Tree<E>::PositionList::Iterator ii = p.u->child.begin(); ii!= p.u->child.end(); ++ii)
        preorder(*ii, pl);
}

template <typename E>
int Tree<E>::depth(Tree<E>::TreeNode& u, Tree<E>::Position& p) //ERROR: Out-of-line definition of 'depth' does not match any declaration in 'Tree<E>' 
{
    if (p.isRoot())
        return 0;
    else
        return 1+depth(u, p.parent());
}


template <typename E>
int Tree<E>::directorySize(Tree<E>::TreeNode& u, Tree<E>::Position& p) //Out-of-line definition of 'directorySize' does not match any declaration in 'Tree<E>'
{
    int s = size(p);
    if (!p.isExternal())
    {
        PositionList ch = p.children();
        for (typename Tree<E>::PositionList::Iterator q = ch.begin(); q != ch.end(); q++)
            s += depth(u, *q);
        cout << name(p) << ": " << s << endl;
}
return s;
}

Tree.h

#ifndef TREE_H
#define TREE_H
#include <cstdlib>
#include "NodeList.cpp"


template <typename E>
class Tree {

public:
    class TreeNode; 

public:
    class Position                 // a node position
    {

    public:
        E operator*();                // get element
        Position parent();
        NodeList<Position> children();
        bool isRoot() const;             // root node?
        bool isExternal() const;             // external node?
        bool operator==(Position p);

        int depth(const Tree& u, const Position& p); //DECLARED(ERROR) - DEPTH OF TWO FUNCTION

        Position(typename Tree<E>::TreeNode *v);
        Position(){}

        typename Tree<E>::TreeNode *u;
        friend class NodeList<Position>;


    };

    typedef NodeList<typename Tree<E>::Position> PositionList;

public:
    class TreeNode
    {
        E e;
        TreeNode *parent;
        typename Tree<E>::PositionList child;
        friend class Tree;
        friend class Tree<E>::Position;

    public:
        TreeNode(){
            parent = NULL;
        }
        TreeNode(E,Tree<E>::Position*, Tree<E>::PositionList*);
        TreeNode& operator=(const TreeNode &t)
        {
            parent = t.parent;
            child = t.child;
            e = t.e;
        }

     };
public:                    // public functions of Tree
    Tree<E>(){
         n = 0;
    }
    int size() const;                // number of nodes
    bool empty() const;                // is tree empty?

    int directorySize(const TreeNode& u, const Position& p); //DECLARED(ERROR) - SIZE OF DIRECTORY

    Position root();
    PositionList Positions();

    void insert(E e, Position p);
    void preorder(Position p ,PositionList &pre_order);

 private:
    TreeNode *r;
    int n;
    friend class Position;
 };


#endif

错误仅出现在 Tree.cpp 文件中。

这里有多个错误:

  1. 参数声明为 const 合格引用,但 const 在定义中被删除。
  2. depth()Tree<E>::Position 而非 Tree<E> 的成员。
  3. depth() 的第一个参数是 Tree<E> 而不是 TreeNode

请注意,将代码放入 .cpp 文件中要么造成混淆(如果打算包含该文件),要么会在使用 template 类 时引起麻烦另一个翻译单位。不过,这个问题与问题无关。