没有从 'int' 到 'student' C++ 的可行转换

no viable conversion from 'int' to 'student' c++

本文关键字:转换 C++ int student      更新时间:2023-10-16

(编辑版)

我目前正在编写二叉搜索树算法。(使用 xCode IDE)我正在从 txt 文件中获取数据并将其作为二进制搜索树插入。这是来自 txt 文件的数据示例。

3800 李,维克多; 2.8

3000 布朗,乔安妮; 4.0

正如您在 student.h 中看到的,有 2 个变量是 id 和 student。Id 包含数据"3800",学生包含"Lee, Victor;2.8".txt 的每一行都被视为一个根。现在,我必须通过一个唯一的键进行搜索,即id(例如"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;
}
/

/findNode 函数

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'到'学生'的可行转换

/

/学生.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; }

我需要 = 运算符来解决这个问题吗?实际上,我创建了 = 运算符,但如果我启用 = 运算符代码,其他函数中的其他等号代码遇到错误。(没有可行的重载"=")

如何修复此错误?谢谢你的帮助。

看看这一行。

ItemType root = nodePtr->getItem().getId();

你觉得这里有什么不对劲吗?