학교 프로젝트의 경우 클래스에서 '우정'을 사용하는 방법을 배우는 동시에 이진 검색 트리를 만들려고합니다. 컴파일하는 동안 발생하는 오류는 다음과 같습니다. [명확성을 위해 오류가 발생한 코드에 주석을 추가합니다.] (BST 클래스의 Node를 중첩 할 수 없다는 것을 명심하십시오. BST 클래스의 Node와 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의 '새'연산자를 사용하여 시도하지만, 난 여전히 그 오류 메시지를 제거 할 수 없습니다. 나는 컴파일러가 그것을 좋아하지 않게 만드는 몇 가지 구문이 빠져있을 수도 있다고 생각한다. 다음은이 과제에 사용되는 파일은 다음과 같습니다 (I는 확보하지 않았기 때문에 아직 사용이 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 오류가 발생 m_left, m_right 및 m_parent를 포함하여 z의 포인터 (z는 방금 작성된 새로운 노드의 포인터)를 수정합니다. 당신이 할당의 대상으로 get_left()
등의 반환을 사용하려면
#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 필드 자체에 할당하려고합니다. – flup
getter 함수는 정적이 아니어야합니다. 객체에 액세스 할 수 없습니다. 할당 문제 때문에 setter 함수를 구현하고 해당 함수를 사용합니다. – Sigroad