no viable conversion from 'int' to 'St

2019-07-12 23:40发布

(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.

1条回答
仙女界的扛把子
2楼-- · 2019-07-13 00:42

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 <.

查看更多
登录 后发表回答