AVL 树最小和最大函数编译错误

发布于 2024-12-11 01:30:47 字数 1069 浏览 0 评论 0原文

我正在构建一个简单的 AVL 树,并从 GCC 收到以下编译器错误:

error: Expected constructor, destructor, or type conversion before '*' token

实现文件中的 min 和 max 函数声明都收到错误。

以下两个成员函数存在问题:

template <typename T>
tree_t<T>::node_t* tree_t<T>::min(node_t* t) const
{
  node_t *temp = t;
  while(temp->left != NULL)
  {
    temp = temp->left;
  }
  return temp;
}

template <typename T>
tree_t<T>::node_t* tree_t<T>::min(tree_t<T>::node_t*) const
{
  node_t *temp = t;
  while(temp->left != NULL)
  {
    temp = temp->left;
  }

  return temp;
}

以下是声明: public:

  node_t*         min(node_t* t) const;

  node_t*         max(node_t* ) const;

这是类和node_t结构声明

template <typename T>
class tree_t
{
  private:
struct node_t
{
T data;
node_t *left;
node_t *right;
int height;
int bal;

    node_t(const T& Element, node_t *lt, node_t *rt, int h = 0)
  : data(Element), left(lt), right(rt), height(h) {};
  };
  node_t * root;

I am constructing a simple AVL tree and receive the following compiler error from GCC:

error: expected constructor, destructor, or type conversion before '*' token

Both the min and max function declarations in the implementation file receive the errors.

the following two member functions are at issue:

template <typename T>
tree_t<T>::node_t* tree_t<T>::min(node_t* t) const
{
  node_t *temp = t;
  while(temp->left != NULL)
  {
    temp = temp->left;
  }
  return temp;
}

template <typename T>
tree_t<T>::node_t* tree_t<T>::min(tree_t<T>::node_t*) const
{
  node_t *temp = t;
  while(temp->left != NULL)
  {
    temp = temp->left;
  }

  return temp;
}

Here is the declaration:
public:

  node_t*         min(node_t* t) const;

  node_t*         max(node_t* ) const;

Here is the class and the node_t struct declaration

template <typename T>
class tree_t
{
  private:
struct node_t
{
T data;
node_t *left;
node_t *right;
int height;
int bal;

    node_t(const T& Element, node_t *lt, node_t *rt, int h = 0)
  : data(Element), left(lt), right(rt), height(h) {};
  };
  node_t * root;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不忘初心 2024-12-18 01:30:47

我无法重现确切的错误。但是,以下是您可以关注的骨架。它不是最好的:-),但它是最接近的(即对原始代码进行最小的更改)。

template <typename T>
class tree_t
{
  public:

    struct node_t
    {
        T data;
        node_t *left;
        node_t *right;
        int height;
        int bal;

        node_t(const T& Element, node_t *lt, node_t *rt, int h = 0)
            : data(Element), left(lt), right(rt), height(h) {
        }
    };

    node_t * root;

    node_t * min( node_t * ) const {
        return 0;  // just for compilation sake
    }

};

int main()
{
    typedef tree_t< int > int_tree;
    typedef int_tree::node_t int_node;

    int_tree my_int_tree;
    int_node my_node( 42, 0, 0 );

    int_node * min_node = my_int_tree.min( & my_node );
    (void) min_node;
}

I could not reproduce the exact error. However, following is a skeleton that you can follow. It is not the best :-), but it is the closest (i.e. making minimum changes to your original code) that compiles.

template <typename T>
class tree_t
{
  public:

    struct node_t
    {
        T data;
        node_t *left;
        node_t *right;
        int height;
        int bal;

        node_t(const T& Element, node_t *lt, node_t *rt, int h = 0)
            : data(Element), left(lt), right(rt), height(h) {
        }
    };

    node_t * root;

    node_t * min( node_t * ) const {
        return 0;  // just for compilation sake
    }

};

int main()
{
    typedef tree_t< int > int_tree;
    typedef int_tree::node_t int_node;

    int_tree my_int_tree;
    int_node my_node( 42, 0, 0 );

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