【问题标题】:How do I overload an operator for a template class in C++?如何在 C++ 中为模板类重载运算符?
【发布时间】:2014-05-24 09:52:43
【问题描述】:

我有一个二叉搜索树类 (BST.h) 和一个节点类 (Node.h),当我在其中存储整数等数据类型时,它们可以正常工作。我的问题是尝试在我的 BST 中存储类对象并使用对象中的属性作为键。我的程序还有一个学生类,其中包含学生 ID 和学生姓名。我将如何在我的学生类中编写运算符重载,以便每次我的 BST 在节点上执行操作时,它都会重载到 student.getID(),而不是对对象本身进行操作。我对重载函数应该是什么样子有一个粗略的想法,但我不知道它应该去哪里或者它的编码是否正确。

//My attempt at an operator overload
bool operator< (const Student &s1, const Student &s2)
{
    return s1.GetID < s2.GetID;
}

//节点.h

#ifndef NODE_H
#define NODE_H

#include <iostream>

using namespace std;

template<class T>
class Node
{
public:

    Node();

    T data;
    Node *left;
    Node *right;
    Node(T);


};

template<class T>
Node<T>::Node()
{
}

template<class T>
Node<T>::Node(T d)
{
    data = d;
    left = NULL;
    right = NULL;
}

#endif //

//BST.h

#ifndef BST_H
#define BST_H
#include <iostream>
#include "Node.h"
#include <string>
using namespace std;

template<class T>
class BST
{
public:

    BST();



    void Insert(T);
    Node<T> *Search(T);
    void preOrder();
    void inOrder();
    void postOrder();



    ~BST();

private:
    Node<T> *root;
    void Insert(T , Node<T> *aNode);
    Node<T> *Search(T, Node<T> *aNode);
    void preOrder(Node<T> *aNode);
    void inOrder(Node<T> *aNode);
    void postOrder(Node<T> *aNode);
};

template<class T>
BST<T>::BST()
{
    root = NULL;
}

template<class T>
void BST<T>::Insert(T data, Node<T> *aNode)
{
    if (data < aNode->data)
    {
        if (aNode->left != NULL)
        {
            Insert(data, aNode->left);
        }
        else
        {
            aNode->left = new Node<T>(data);
            aNode->left->left = NULL;
            aNode->left->right = NULL;
        }
    }
    else
    {
        if (data >= aNode->data)
        {
            if (aNode->right != NULL)
            {
                Insert(data, aNode->right);
            }
            else
            {
                aNode->right = new Node<T>(data);
                aNode->right->left = NULL;
                aNode->right->right = NULL;
            }
        }
    }
}

template<class T>
void BST<T>::Insert(T data)
{
    if (root != NULL)
    {
        Insert(data, root);
    }
    else
    {
        root = new Node<T>(data);
        root->left = NULL;
        root->right = NULL;
    }
}

template<class T>
Node<T>* BST<T>::Search(T data, Node<T> *aNode)
{
    if (aNode != NULL)
    {
        if (data == aNode->data)
        {
            return aNode;
        }

        if (data < aNode->data)
        {
            return Search(data, aNode->left);
        }
        else
        {
            return Search(data, aNode->right);
        }

    }
    else
    {
        return NULL;
    }
}

template<class T>
Node<T>* BST<T>::Search(T data)
{
    return Search(data, root);
}

template<class T>
void BST<T>::preOrder()
{
    preOrder(root);
}

template<class T>
void BST<T>::preOrder(Node<T> *aNode)
{
    if (aNode != NULL)
    {
        cout << aNode->data << " ";
        preOrder(aNode->left);
        preOrder(aNode->right);

    }
}

template<class T>
void BST<T>::inOrder()
{
    inOrder(root);
}

template<class T>
void BST<T>::inOrder(Node<T> *aNode)
{
    if (aNode != NULL)
    {
        inOrder(aNode->left);
        cout << aNode->data << " ";
        inOrder(aNode->right);
    }
}

template<class T>
void BST<T>::postOrder()
{
    postOrder(root);
}

template<class T>
void BST<T>::postOrder(Node<T> *aNode)
{
    if (aNode != NULL)
    {
        postOrder(aNode->left);
        postOrder(aNode->right);
        cout << aNode->data << " ";
    }
}

template<class T>
BST<T>::~BST()
{
}


#endif // !BST_H

//学生.h

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
    Student();
    Student(string, int);
    ~Student();
    int Student::GetID();


private:
    string name;
    int ID;
};

inline int Student::GetID()
{
    return ID;
}

【问题讨论】:

  • 您的接线员看起来不错。除了,您正在比较每个实例的 GetID 函数,而不是调用该函数并比较返回值。 Student 不是模板类,所以你的问题的标题让我很困惑。

标签: c++ templates object operator-overloading binary-search-tree


【解决方案1】:

您似乎在询问 operator&lt;Students 的关系,但 Student 不是类模板,因此您的帖子标题令人费解。

正如其他人指出的那样,您的 operator&lt; 几乎是正确的,只是您必须实际调用 GetID() 而不是比较指向成员函数的指针。

但是,在您修复 GetID 之前,这将无法正常工作。而不是int Student::GetID(); 应该是:

int GetID() const;

const 意味着它可以在通过 const 引用传递的对象上调用,就像您在 operator&lt; 实现中所做的那样。在类中声明函数时不要重复Student::。 (在类定义之外定义类成员时使用它)。

【讨论】:

    【解决方案2】:

    在你的 Student 类中声明它为友元函数,在你的其他成员函数旁边

    friend bool operator < (Student& s1, Student& s2);
    

    您的实现是正确的,它应该在同一个头文件中的 Student 类之外。

    【讨论】:

    • 不应该是朋友,应该是const,也带const引用。
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 2017-10-29
    • 2012-11-14
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多