[英]Creating a new Node for a binary search tree
對於一個學校項目,我試圖同時制作一個二叉搜索樹,我們應該學習如何在課堂上使用“友誼”。 我在編譯時遇到的錯誤是:[為了清楚起見,我在代碼中添加了注釋,錯誤的來源是(請記住,我不允許將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'運算符,但仍然無法擺脫這些錯誤消息。 我相信我可能會缺少一些使編譯器不喜歡的語法。 這是此作業中使用的文件:(請注意,由於我在項目中還沒有到此為止,所以尚未使用某些功能。)Node.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
Node.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(為了清楚起見,我在注釋中標出了發生錯誤的位置)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
}
}
如果要將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直接訪問這些字段。 這似乎是練習的重點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.