За училищен проект се опитвам да направя двоично дърво за търсене в същото време, когато трябва да научим как да използваме „приятелство“ в класовете. Грешките, които получавам, докато компилирам, са: [Поставям коментари в кода, откъдето идват грешките, за яснота] (Имайте предвид, че не ми е разрешено да влагам 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, но все още не мога да се отърва от тези съобщения за грешка. Вярвам, че може да ми липсват няколко синтаксиса, които карат компилатора да не го харесва. Ето файловете, използвани в това задание: (Обърнете внимание, че някои функции все още не се използват, тъй като не съм стигнал толкова далеч в проекта.) 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
}
}