c++ - 没有从 'int' 到 'Student' 的可行转换

标签 c++ class templates operator-keyword

(编辑版)

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

3800 李维克多; 2.8

3000 布朗,乔安妮; 4.0

正如您在 student.h 中看到的,有 2 个变量,分别是 id 和 student。 Id 包含数据“3800”,student 包含“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);
        }

我将输入数据放入结果中并尝试搜索数据。

//公共(public)函数定义中我的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; }

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

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

最佳答案

错误只是说你没有 operator=定义允许您分配 intstringStudent . getId()getName()分别返回intstring你正试图将他们的结果分配给 ItemType在你的情况下是 Student .

您没有采用 int 的 Student 构造函数或 string要么,这让我觉得你可能不想从 int 进行隐式转换或 stringStudent (我个人不建议这样做)。

所以要么:

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

修复的一个选项是:

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

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

你的 operator= 的原因不工作是因为你没有拍 const引用作为参数,因此它不能与 const 一起使用= 右侧的对象,产生新的错误。但是,修复它不会使其适用于 intstring只要您没有采用这些的隐式构造函数。

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

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

或者你的operator>= , operator>operator==实际上与<相比.

关于c++ - 没有从 'int' 到 'Student' 的可行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005119/

相关文章:

c++ - 在现代 OpenGL 中计算光的每个片段法线

c++ - 用于集成 Visual Studio 单元测试的 CMake 文件

python - 最佳实践 python 类

c++ - 默认模板参数的范围是什么?

c++ - 模板化使用无法选择模板函数以用作 Visual Studio 中的参数

c++ - 从参数中选择模板返回类型

c++ - boost::mutex 和 boost::timed_mutex 的区别

c++ - 在分配器感知的 STL 类中,为什么分配器不是模板模板参数?

c++ - 访问派生类成员的基类函数

python - 学校垄断作业的一部分不会输出任何内容(初学者,Python)