没有可行的转换从'int'到'学生' [英] no viable conversion from 'int' to 'Student'

查看:123
本文介绍了没有可行的转换从'int'到'学生'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(编辑版)



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



3800 Lee,Victor; 2.8



3000 Brown,Joanne; 4.0



正如你可以在student.h中看到的,有两个变量是id和student。 ID包含数据3800,学生包含Lee,Victor; 2.8。 txt的每一行都认为是一个根。
现在,我必须通过一个唯一的密钥id(例如3800)搜索,并打印出是否在树中找到。
我有5个文件,BinaryNode.h,BinaryTree.h,BinarySearchTree.h,Student.h,main.cpp。
所有的二进制头文件都使用模板,Student.h没有模板。
所以,这里是我的int main。

  int main()
{
BinarySearchTree< ; Student>树;
getData(tree); (我没有包括getData部分,但是考虑到树有txtfile数据。)

bool found = false;
char input;
do
{
cout<< 输入一个关键字以访问相应的菜单。 << endl<< endl
cout<< T - 将树打印为缩进列表< endl
cout<< S - 通过唯一键(学生ID)搜索< endl
cout<< B-树宽度 - 第一遍:按级别打印< endl
cout<< D - Depth-First Traversals:inorder,preorder,postorder< endl
cout<< R - 找到最长的分支并打印它(从叶到根)< endl
cout<< H - 帮助< endl
cout<< Q-Quit< endl<< endl

cout<< 输入:;
cin>>输入;
cout<< endl

if(input =='T'|| input =='S'|| input =='B'|| input =='D'|| input =='R'||输入=='H'|| input =='Q'|| input =='A')
{
if(input =='T')
{
// print tree as indented
}
else if(input =='S')
{
//按学生ID搜索
Student * result = new Student ;
int id;
cout<< 输入学生ID以搜索匹配的学生。 << endl
cin>> ID;
result-> setId(id);
found = tree.getEntry(* result);
}

将输入数据转换为结果,并尝试搜索数据。 / p>

//公共函数定义中的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到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);
}
/ *
学生& 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; }

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



如何解决这个错误?
感谢您的帮助。

解决方案

错误只是说您没有 = 定义为允许您将 int 字符串分配给 Student getId() getName()分别返回 int string ,并且您尝试将其结果分配给 ItemType ,在您的情况下为 Student c。



您没有学生构造函数接受 int code> string 或者,这让我想你可能不想从 int string 学生(个人我不会建议)。



/ p>


  • 您的代码在语义上是错误的,您想要做的是调用 getId() getName()(我认为是这种情况)

  • getId code>和 getName()函数错误,并应返回 Student 以匹配模板要求

  • 您的模板不适合您的对象:)



一个选项是:

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

要调用构造函数 int code> string 。注意,我改变了变量名,因为你的代码没有什么意义:你把 result.getId()赋值给 result c>你刚刚定义的



operator = t取一个 const 引用作为参数,因此它不能使用 const code> = ,生成新错误。然而,修复它不会使它工作 int string 只要你没有隐式构造函数。



代码中还有很多其他奇怪的东西,比如你的 getStudent()不是一个getter,你应该命名不同,并且可能使用 istream 引用作为参数,或者在构造函数中使用assignement而不是初始化列表:

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

> operator> = , operator> operator == $ c>< 。


(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 an int or a string to a Student. getId() and getName() returns respectively int and string and you're trying to assign their result to ItemType which in your case is Student.

You don't have a Student constructor taking an int or a string either, which makes me think you may not want an implicit conversion from int or string to Student (personally I wouldn't advise it).

So either:

  • your code is wrong semantically and what you want to do is call another function than getId() and getName() ( I think that's the case )
  • your getId() and getName() functions are wrong and should return Student to match the template requirements
  • your template is not adapted to your object :)

One option to fix is:

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

To call the constructor taking int and string. Note that I changed the variables names because your code doesn't really make sense: you assign result.getId() to result that you just defined !!

The reason why your operator= is not working is that you don't take a const reference as parameter and therefore it can't work with const object on the right side of the =, generating new errors. However, fixing it won't make it work for int and string 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 a istream reference as parameter, or the usage of assignement instead of initialization list in constructors:

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

Or that both your operator>=, operator> and operator== actually compares with <.

这篇关于没有可行的转换从'int'到'学生'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆