(Edited version)
I am currently coding Binary Search Tree algorithm. (using xCode IDE) I'm taking the data from txt file and insert it as binarysearchtree. This is the sample of data from txt file.
3800 Lee, Victor; 2.8
3000 Brown, Joanne; 4.0
As you can see in student.h, there are 2 variables which are id and student. Id contains the data "3800" and student contains "Lee, Victor; 2.8". Each line of the txt consider as one root. Now, I have to search by a unique key which is id(Ex. "3800") and print out if it is found in the tree. I have 5 files, BinaryNode.h, BinaryTree.h, BinarySearchTree.h, Student.h, main.cpp. All 3 of the binary header file are using template and Student.h has no template. So, here is my int main.
int main()
{
BinarySearchTree<Student> tree;
getData(tree); (I didn't include getData part, but consider tree has txtfile data.)
bool found = false;
char input;
do
{
cout << "Enter a key letter to access to a corresponding menu." << endl << endl;
cout << "T – Print tree as an indented list" << endl;
cout << "S – Search by a unique key (student ID)" << endl;
cout << "B – Tree Breadth-First Traversal: Print by level" << endl;
cout << "D – Depth-First Traversals: inorder, preorder, postorder" << endl;
cout << "R – Find the longest branch and print it (from leaf to root)" << endl;
cout << "H – Help" << endl;
cout << "Q – Quit" << endl << endl;
cout << "Input: ";
cin >> input;
cout << endl;
if(input == 'T' || input == 'S' || input == 'B' || input == 'D' || input == 'R' || input == 'H' || input == 'Q' || input == 'A')
{
if(input == 'T')
{
//print tree as indented
}
else if(input == 'S')
{
//search by student ID
Student *result = new Student;
int id;
cout << "Enter the student ID to search the matching student." << endl;
cin >> id;
result->setId(id);
found = tree.getEntry(*result);
}
I cin the input data into result and tried to search for the data.
//My getEntry function in public function definition
template<class ItemType>
bool BinarySearchTree<ItemType>::getEntry(ItemType& anEntry)
{
BinaryNode<ItemType>* returnedItem = findNode(BinaryTree<ItemType>::rootPtr, anEntry);
if (returnedItem)
{
anEntry = returnedItem->getItem();
return true;
}
else return false;
}
//findNode function
template<class ItemType>
BinaryNode<ItemType>*
BinarySearchTree<ItemType>::findNode(BinaryNode<ItemType>* nodePtr,
ItemType & target)
{
ItemType result = result.getId(); <------- ******error here*******
ItemType root = nodePtr->getItem().getId();
if (nodePtr == nullptr)
return nullptr;
if (result == root)
return root;
if (result > root)
root = findNode(nodePtr->getRightPtr(), target);
else
root = findNode(nodePtr->getLeftPtr(), target);
return root;
}
I have an error on my findNode function.
->No viable conversion from 'int' to 'Student'
//Student.h
class Student
{
private:
int id;
std::string name;
public:
Student() { id = 0; name = ""; }
Student(int newId, std::string newName) { id = newId; name = newName; }
friend bool operator >= (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator == (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator < (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator > (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
/*
Student& operator = (Student& t_id)
{
if(this != &t_id)
id = t_id.getId();
return *this;
}
*/
void getStudent() { std::cin >> id; }
int getId() const { return id; }
void setId(int t_id) { id = t_id; }
std::string getName() const { return name; }
void setName(std::string t_name) { name = t_name; }
//ItemType getGpa() const { return gpa; }
//virtual void setGpa(std::string t_gpa) { gpa = t_gpa; }
Do I need = operator to fix that problem? Actually, I created the = operator but if I enable that = operator code, the other codes with equal sign that are in other functions encounter errors. (no viable overloaded '=')
How can I fix this errors? Thank you for your helping.
The error simply says that you have no
operator=
defined that allows you to assign anint
or astring
to aStudent
.getId()
andgetName()
returns respectivelyint
andstring
and you're trying to assign their result toItemType
which in your case isStudent
.You don't have a Student constructor taking an
int
or astring
either, which makes me think you may not want an implicit conversion fromint
orstring
toStudent
(personally I wouldn't advise it).So either:
getId()
andgetName()
( I think that's the case )getId()
andgetName()
functions are wrong and should returnStudent
to match the template requirementsOne option to fix is:
To call the constructor taking
int
andstring
. Note that I changed the variables names because your code doesn't really make sense: you assignresult.getId()
toresult
that you just defined !!The reason why your
operator=
is not working is that you don't take aconst
reference as parameter and therefore it can't work withconst
object on the right side of the=
, generating new errors. However, fixing it won't make it work forint
andstring
as long as you don't have an implicit constructor taking those.There are many other strange things in your code, like your
getStudent()
method that is not a getter, you should name it differently and probably make it take aistream
reference as parameter, or the usage of assignement instead of initialization list in constructors:Or that both your
operator>=
,operator>
andoperator==
actually compares with<
.