【问题标题】:no viable conversion from 'int' to 'Student'没有从“int”到“Student”的可行转换
【发布时间】: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,ItemTypeStudent。那么这应该如何工作呢?

标签: c++ class templates operator-keyword


【解决方案1】:

错误只是说您没有定义operator=,允许您将intstring 分配给StudentgetId()getName() 分别返回 intstring 并且您试图将它们的结果分配给 ItemType 在您的情况下是 Student

您也没有采用 intstring 的 Student 构造函数,这让我认为您可能不希望从 intstringStudent 的隐式转换(我个人会'不建议它)。

所以要么:

  • 您的代码在语义上是错误的,您要做的是调用除 getId()getName() 之外的另一个函数(我认为是这样)
  • 您的 getId()getName() 函数有误,应返回 Student 以符合模板要求
  • 您的模板不适合您的对象 :)

修复的一个选项是:

ItemType x(a.getId(), a.getName());

intstring 调用构造函数。请注意,我更改了变量名称,因为您的代码实际上没有意义:您将 result.getId() 分配给您刚刚定义的 result !!

您的operator= 不起作用的原因是您没有将const 引用作为参数,因此它不能与= 右侧的const 对象一起使用,生成新的错误。但是,只要您没有隐式构造函数接受它们,修复它就不会使其适用于 intstring

您的代码中还有许多其他奇怪的东西,例如您的 getStudent() 方法不是 getter,您应该将其命名为不同的名称,并可能使其将 istream 引用作为参数,或者使用赋值而不是构造函数中的初始化列表:

Student() : id(0), name("") {}
Student(int newId, std::string newName) : ud(newId), name(newName) {}

或者你的operator&gt;=operator&gt;operator==实际上都与&lt;比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多