I have tried this solution but I am not able to understand it for my case.
My project Structure is
- ConsoleApplication1.cpp (main)
- BST.h
- BST.cpp
- help.cpp
Now my main is
#include "BST.h"
#include "help.cpp"
int main()
{
BST b1;
b1.insertion(5);
b1.insertion(3);
b1.insertion(2);
b1.insertion(5);
b1.insertion(7);
b1.insertion(9);
cout << predecessor(b1, 9)->data;
My BST.h is
#pragma once
#include <iostream>
using namespace std;
struct node {
int data;
node* leftchild, * rightchild, * parent;
};
class BST {
private:
node* root;
public:
BST();
node* rooter();
void insertion(int);
void outleftandright();
node* search(int x);
};
and BST.cpp is
#include "BST.h"
node* BST::search(int x)
{
if (x == root->data)
{
return root;
}
node* temp = root;
while (temp)
{
if (x < temp->data)
{
temp = temp->leftchild;
}
else if (x > temp->data)
{
temp = temp->rightchild;
}
else if (x == temp->data)
{
return temp;
}
}
}
void BST::outleftandright() {
node* temp = root;
cout << "LEFT\n";
while (temp)
{
cout << temp->data << endl;
temp = temp->leftchild;
}
temp = root;
cout << "Right\n";
while (temp)
{
cout << temp->data << endl;
temp = temp->rightchild;
}
}
BST::BST()
{
root = nullptr;
}
node* BST::rooter()
{
return root;
}
void BST::insertion(int data)
{
if (root == nullptr)
{
root = new node;
root->data = data;
root->leftchild = nullptr;
root->rightchild = nullptr;
}
else {
node* temp = root;
while (true)
{
if (data <= temp->data)
{
if (temp->leftchild == nullptr)
{
temp->leftchild = new node;
temp->leftchild->parent = temp;
temp = temp->leftchild;
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
break;
}
temp = temp->leftchild;
}
else if (data > temp->data)
{
if (temp->rightchild == nullptr)
{
temp->rightchild = new node;
temp->rightchild->parent = temp;
temp = temp->rightchild;
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
break;
}
temp = temp->rightchild;
}
}
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
}
}
and help.cpp is
#include "BST.h"
node* minimum(node* b1)
{
while (b1->leftchild != nullptr)
{
b1 = b1->leftchild;
}
return b1;
}
node* maximum(node* temp)
{
while (temp->rightchild != nullptr)
{
temp = temp->rightchild;
}
return temp;
}
node* successor(BST b1, int x)
{
node* temproot = b1.search(x);
node* temproot1 = temproot;
if (temproot->rightchild != nullptr)
{
return minimum(temproot->rightchild);
}
else
{
node* y = temproot->parent;
while (y != nullptr && temproot == y->rightchild)
{
temproot = y;
y = temproot->parent;
}
if (y == nullptr)
{
return maximum(temproot1);
}
else {
return y;
}
}
}
node* predecessor(BST b1, int x)
{
node* temproot = b1.search(x);
node* temproot1 = temproot;
if (temproot->leftchild != nullptr)
{
return maximum(temproot->leftchild);
}
else
{
node* y = temproot->parent;
while (y != nullptr && temproot == y->leftchild)
{
temproot = y;
y = temproot->parent;
}
if (y == nullptr)
{
return minimum(temproot1);
}
else {
return y;
}
}
}
Now When I did not had help.cpp and the functions inside help.cpp were in my Consol1Application1.cpp, it was working fine, but now it is giving error
1>help.obj : error LNK2005: "struct node * __cdecl maximum(struct node *)" (?maximum@@YAPEAUnode@@PEAU1@@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl minimum(struct node *)" (?minimum@@YAPEAUnode@@PEAU1@@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl predecessor(class BST,int)" (?predecessor@@YAPEAUnode@@VBST@@H@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl successor(class BST,int)" (?successor@@YAPEAUnode@@VBST@@H@Z) already defined in ConsoleApplication1.obj
1>C:\Users\Ahmad Mustafa Anis\source\repos\Week 13 Assign\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe : fatal error LNK1169: one or more multiply defined symbols found
How to deal with it? Thanks