【问题标题】:Creating a new Node for a binary search tree为二叉搜索树创建新节点
【发布时间】:2013-11-27 10:12:34
【问题描述】:

对于一个学校项目,我正在尝试制作二叉搜索树,同时我们应该学习如何在课堂上使用“友谊”。我在编译时遇到的错误是:[为了清楚起见,我将 cmets 放在错误源自的代码中](请记住,我不允许将 Node 嵌套在 BST 类中,它们都应该位于单独的文件和类中为了这个编程任务)

BST.cpp: In member function `void BST::insert(std::string, std::string)':
BST.cpp:51: error: non-lvalue in assignment
BST.cpp:58: error: non-lvalue in assignment
BST.cpp:62: error: non-lvalue in assignment
makefile.txt:9: recipe for target `BST.o' failed
make: *** [BST.o] Error 1

我尝试在 BST.cpp 和 Node.cpp 中使用“new”运算符,但仍然无法摆脱这些错误消息。我相信我可能遗漏了一些使编译器不喜欢它的语法。以下是本次作业中使用的文件:(请注意,有些功能尚未使用,因为我在项目中还没有走得那么远。) 节点.h

#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class BST;
class Node
{
public:
    Node(string key, string data)
    {m_key = key; m_data = data;}
    ~Node();
    static string get_key(); //takes in ptr to node and returns its key
    static string get_data(); //takes in ptr to node and returns its data
    static Node* get_left(); //takes in ptr to node and returns its left child pointer
    static Node* get_right(); //takes in ptr to node and returns its right child pointer
    static Node* get_parent(); //takjes in ptr to node and returns its parent pointer
    static Node* create_node(string key, string data);
    static void destroy_node();

private:
    string m_key;
    string m_data;
    Node *m_left;
    Node *m_right;
    Node *m_parent;
};


#endif // NODE_H_INCLUDED

节点.cpp

#include "Node.h"

static string Node::get_key()
{
    return m_key;
}
static string Node::get_data()
{
    return m_data;
}
static Node* Node::get_left()
{
    return m_left;
}
static Node* Node::get_right()
{
    return m_right;
}
static Node* Node::get_parent()
{
    return m_parent;
}
static Node* Node::create_node(string key, string data)
{
    Node* ptr = new Node(key, data);
    ptr->m_left = NULL;
    ptr->m_right = NULL;
    ptr->m_parent = NULL;
    return ptr;
}

到目前为止,我的意图是让 Node::create_Node 创建一个新节点,使所有指针无效,最后将节点的指针传回 BST.cpp,以便可以修改指针并将其插入树中。下面是 BST.cpp 和 BST.h(为了您的清楚,我把 cmets 放在了发生错误的地方) BST.h:

#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class BST
{
public:
    BST()
    {m_root = NULL;}
    ~BST();
    void insert(string key, string data);
    void find(string key);
    void remove(string key, string data);
    void print();
    friend class Node;
private:
    Node* m_root;

};

#endif // BST_H_INCLUDED

最后,BST.cpp(发生错误的地方)当我尝试修改 z 的指针(z 是刚刚创建的全新节点的指针)包括它的 m_left、m_right 和 m_parent 时,就会发生错误。

#include "BST.h"
#include "Node.h"

void BST::insert(string key, string data)
{
    Node* x = m_root;
    Node* y = NULL;
    Node* z = Node::create_node(key, data);
    while(x != NULL)
    {
        y = x;
        if(key < x->get_key())
        {
            x = x->get_left();
        }
        else
        {
            x = x->get_right();
        }
    }
    z->get_parent() = y; //error: non-lvalue in assignment
    if(y == NULL)
    {
        m_root = z;
    }
    else if(z->get_key() < y->get_key())
    {
        y->get_left() = z; //error: non-lvalue in assignment
    }
    else
    {
        y->get_right() = z; //error: non-lvalue in assignment
    }
}

【问题讨论】:

  • getter 的结果是没有左值,你不能给它分配一个新的值。相反,您希望分配给 m_left 字段本身。
  • getter 函数不应该是静态的,它们无权访问对象,对于分配问题,我将实现 setter 函数并使用它们

标签: c++ pointers binary-search-tree new-operator friend


【解决方案1】:

如果您想使用get_left() 等的返回作为分配的目标,那么您必须返回一个引用。

然而,更大的错误是您出于某种原因将所有这些方法设为静态。那也行不通。

Node*& Node::get_left()
{
    return m_left;
}
Node*& Node::get_right()
{
    return m_right;
}
Node*& Node::get_parent()
{
    return m_parent;
}

但是,既然重点是学习如何使用友谊,您可能应该删除这些方法并将 BST 声明为 Node 的朋友,并让 BST 直接访问这些字段。这似乎是练习的重点。

【讨论】:

  • 我尝试让他们成为“朋友”,但它仍然不允许我从 BST 访问 Node 的私有成员。 (这是几个小时前,很难记住)。这就是我制作这些辅助函数的原因,您是说我不需要 Node.cpp 中的辅助函数或 BST.cpp 中的“#include“Node.h””吗?这就是让我感动的事情,我对继承和友谊知之甚少。
  • 你不是让“他们”成为朋友,而是让 BST 类成为 Node 类的朋友。所有friend class BST;class Node { ... }; 内的某个地方@
  • 所以反过来,因为我尝试在另一个文件中使用“朋友类节点”,但似乎什么也没做。
  • 是的,Node 类将友谊授予 BST 类。如果反过来,任何班级都可以声称是它喜欢的任何班级的朋友。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多