I have to implement a general tree en C++ for one of my class, and I come across a problem I don't understand.
I have two classes, EmployeeNode and EmpoyeeTree.
EmployeeNode contains the data elements needed for the work : a string name, an EmployeeNode parent and a List<EmployeeNode> children which is a linked list I implemented before, supposedly working with any template object.
Here is my code for the moment:
class EmployeeTree;
class EmployeeNode {
public:
EmployeeNode(std::string name, EmployeeNode* parent, List<EmployeeNode>* child);
~EmployeeNode();
void setChild(EmployeeNode newEmployee) {child->insert(newEmployee);}
List<EmployeeNode>* getChild() {return(child);}
bool hasChild() {return (child != 0);}
std::string getName() {return name;}
private:
std::string name;
EmployeeNode *parent;
List<EmployeeNode> *child;
};
EmployeeNode::EmployeeNode(std::string employeeName, EmployeeNode* employeeParent, List<EmployeeNode>* employeeChildren)
:name(employeeName), parent(employeeParent), child(employeeChildren)
{
employeeChildren = new List<EmployeeNode>;
}
EmployeeNode::~EmployeeNode() {}
class EmployeeTree {
public:
EmployeeTree();
~EmployeeTree();
void hireEmployee(EmployeeNode *newEmployee);
void hireEmployee(EmployeeNode* boss, std::string newEmployee);
EmployeeNode find(std::string employee);
void print(EmployeeTree Tree);
private:
int level, age;
EmployeeNode *root;
};
EmployeeTree::EmployeeTree()
:root(0)
{}
EmployeeTree::~EmployeeTree()
{}
void EmployeeTree::hireEmployee(EmployeeNode *newEmployee)
{
root = newEmployee;
}
void EmployeeTree::hireEmployee(EmployeeNode* boss, std::string newEmployee)
{
EmployeeNode* newChild;
if (!boss->hasChild()){
newChild = new EmployeeNode(newEmployee, boss, 0);
boss->setChild(*newChild);
}
else {
newChild = new EmployeeNode(newEmployee, boss, boss->getChild());
boss->setChild(*newChild);
}
}
EmployeeNode EmployeeTree::find(std::string employee) {
if(root->getName() == employee)
return *root;
else if (root->getChild()) {
List<EmployeeNode> *children = root->getChild();
children->gotoBeginning();
for(children->getCursor(); children->getCursor().getName() == employee ;children->gotoNext())
*root = children->getCursor();
return(*root);
}
else {std::cout << "Boss not found in employee tree." << std::endl;}
return(*root);
}
For now, I'm just trying some basic commands to test my work. I first create the root EmployeeNode with hireEmployee(EmployeeNode *newEmployee), and then I try to add a child to that with hireEmployee(EmployeeNode *boss, std::string newEmployee), but I get an error telling me I try to add the child in a non-existing children list. I checked, but I don't understand where or what my error is.
When debugging with breakpoints, I found that every time it is created, the List<EmployeeNode> is automatically destructed after.
I think I played too much with pointers without fully understanding them, but now I stuck with this.
boss->setChild(*newChild)probably does not do what you want it to do.employeeChildren = new List<EmployeeNode>;? At present it just overwrite the function argument, not a member variable, so the only meaninful thing it does is leak memory.