Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为二叉搜索树创建新节点_C++_Pointers_Binary Search Tree_New Operator_Friend - Fatal编程技术网

C++ 为二叉搜索树创建新节点

C++ 为二叉搜索树创建新节点,c++,pointers,binary-search-tree,new-operator,friend,C++,Pointers,Binary Search Tree,New Operator,Friend,对于一个学校项目,我试图在学习如何在课堂上使用“友谊”的同时制作一个二叉搜索树。我在编译时遇到的错误是:[为了清楚起见,我将注释放在错误源代码中](请记住,我不允许将节点嵌套在BST类中,为了完成此编程任务,它们都应该位于单独的文件和类中) 我尝试在BST.cpp和Node.cpp中使用“new”操作符,但仍然无法消除这些错误消息。我相信我可能遗漏了一些语法,使编译器不喜欢它。以下是本作业中使用的文件:(注意,一些函数尚未使用,因为我在项目中还没有做那么多。) Node.h 到目前为止,我的目的

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

我尝试在BST.cpp和Node.cpp中使用“new”操作符,但仍然无法消除这些错误消息。我相信我可能遗漏了一些语法,使编译器不喜欢它。以下是本作业中使用的文件:(注意,一些函数尚未使用,因为我在项目中还没有做那么多。) Node.h

到目前为止,我的目的是让Node::create_Node创建一个新节点,清空所有指针,最后将节点的指针传递回BST.cpp,以便修改指针并将其插入到树中。下面是BST.cpp和BST.h(为了清楚起见,我在出现错误的地方添加了注释) 英国夏令时:

#如果包括BST#H#
#定义包含的BST_H_
#包括
#包括
使用名称空间std;
BST级
{
公众:
BST()
{m_root=NULL;}
~BST();
无效插入(字符串键、字符串数据);
无效查找(字符串键);
作废删除(字符串键、字符串数据);
作废打印();
好友类节点;
私人:
节点*m_根;
};
#endif//BST_H_包括在内
最后,BST.cpp(发生错误的地方)当我试图修改z的指针(z是一个指针,是刚刚创建的全新节点)时,会发生错误,包括它的m_left、m_right和m_父节点

#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
    }
}
#包括“BST.h”
#包括“Node.h”
void BST::insert(字符串键、字符串数据)
{
Node*x=m_根;
节点*y=NULL;
Node*z=Node::创建_节点(键、数据);
while(x!=NULL)
{
y=x;
如果(键get_键())
{
x=x->向左走();
}
其他的
{
x=x->get_right();
}
}
z->get_parent()=y;//错误:赋值中非左值
如果(y==NULL)
{
m_根=z;
}
否则如果(z->get_key()get_key())
{
y->get_left()=z;//错误:赋值中非左值
}
其他的
{
y->get_right()=z;//错误:赋值中非左值
}
}

如果要使用返回的
get_left()
等作为分配的目标,则必须返回引用

然而,更大的错误是,由于某种原因,您使所有这些方法都是静态的。那也不行

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

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

getter的结果是没有左值,您不能为其分配新值。您希望分配给m_left字段本身,而不是。getter函数不应该是静态的,它们没有访问对象的权限,对于分配问题,我将实现setter函数并使用thoseI尝试使它们成为“朋友”,但它仍然不允许我从BST访问节点的私有成员。(这是几个小时前的事,很难记住)。这就是我制作这些帮助函数的原因,你是想说我不需要Node.cpp或“#include”Node.h”中的帮助函数吗'在BST.cpp中?正是这件事让我明白了,我对继承和友谊几乎一无所知。你不会让'他们'成为朋友,而是让BST类成为节点类的朋友。所有
朋友类BST;
类节点{…}中的某个地方
反过来说,因为我尝试在另一个文件中使用“friend class Node”,但似乎没有效果。是的,Node类将友谊授予BST类。如果是另一种方式,任何类都可以声称自己是它喜欢的任何类的朋友。
#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;
}
#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
#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
    }
}
Node*& Node::get_left()
{
    return m_left;
}
Node*& Node::get_right()
{
    return m_right;
}
Node*& Node::get_parent()
{
    return m_parent;
}