【发布时间】:2016-06-30 12:41:55
【问题描述】:
(修改版)
我目前正在编写二叉搜索树算法。 (使用 xCode IDE) 我从 txt 文件中获取数据并将其作为 binarysearchtree 插入。 这是txt文件中的数据样本。
3800 李,维克多; 2.8
3000 布朗,乔安妮; 4.0
正如您在 student.h 中看到的,有 2 个变量,即 id 和 student。 Id 包含数据“3800”,学生包含“Lee, Victor; 2.8”。 txt 的每一行都视为一个根。 现在,我必须通过一个唯一键进行搜索,即 id(Ex. "3800") 并打印出它是否在树中找到。 我有 5 个文件,BinaryNode.h、BinaryTree.h、BinarySearchTree.h、Student.h、main.cpp。 所有 3 个二进制头文件都使用模板,Student.h 没有模板。 所以,这是我的 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);
}
我将输入数据输入结果并尝试搜索数据。
//我在公共函数定义中的getEntry函数
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;
}
//查找节点函数
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;
}
我的 findNode 函数出现错误。
->没有从“int”到“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; }
我需要 = 运算符来解决这个问题吗? 实际上,我创建了 = 运算符,但如果启用 = 运算符代码, 其他函数中的其他等号代码 遇到错误。 (没有可行的重载'=')
如何解决此错误? 感谢您的帮助。
【问题讨论】:
-
在哪里你得到错误?错误消息中没有其他信息说明?请尝试创建Minimal, Complete, and Verifiable Example 并展示给我们。
-
很抱歉。我刚刚编辑了错误发生的位置。我指出的那一行同时有 2 个错误。
-
那么
ItemType是什么?result是什么?result.getId()返回什么? 请尝试创建一个MCVE,让我们看到类似了解所有内容的东西真的很重要。否则我们所能做的就是猜测,而且大多数情况下都猜错了。 -
Joachim 是对的:MCVE 帮助我们正确回答并帮助您提出更好的问题。但更好的是,它通常允许您在准备问题的同时自己回答问题,并在此过程中学到很多东西。
-
你希望这段代码做什么
ItemType result = result.getId();?getId函数返回一个 int,ItemType是Student。那么这应该如何工作呢?
标签: c++ class templates operator-keyword