2013-11-27 40 views
2

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

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“新”经营者,但我仍然无法摆脱那些错误信息。我相信我可能会错过一些使编译器不喜欢它的语法。以下是此任务所用的文件:(的arent尚未使用注意一些功能,因为我还没有得到那么远的项目。) 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 
    } 
} 
+0

getter的结果不是左值,您不能为其分配新的值。相反,您希望将其分配给m_left字段本身。 – flup

+0

getter函数不应该是静态的,它们不能访问对象,因为赋值问题我会​​实现setter函数并使用那些 – Sigroad

回答

0

如果你想使用的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访问。这似乎是这个练习的重点。

+0

我试图让它们成为'朋友',但它仍然不会让我访问私有BST的Node成员。 (这是几个小时前,它很难记住)。这就是为什么我做了这些帮助函数,你是否想说我不需要Node.cpp中的辅助函数或BST.cpp中的#include“Node.h”?这就是我所得到的东西,我对继承和友谊毫无所知。 – user3040019

+0

你不会让他们成为朋友,你会让BST类成为Node类的朋友。所有的'朋友类BST;'在类节点{...};' – john

+0

所以反过来,因为我尝试在另一个文件中使用'朋友类节点',似乎什么都不做。 – user3040019