如何在C ++中为模板类重载操作符?

时间:2014-05-24 09:52:43

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

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

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

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

// Student.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;
}

2 个答案:

答案 0 :(得分:1)

您似乎在问operator< StudentStudent,但operator<不是课程模板,因此您的帖子标题令人费解。

正如其他人指出的那样,您的GetID()几乎是正确的,除非您必须实际调用GetID而不是将指针与成员函数进行比较。

在您修复int Student::GetID();之前,这将无法使用。它不应该是int GetID() const; 而应该是:

const

operator<表示可以在const引用传递的对象上调用它,就像在Student::实现中一样。在类中声明函数时,不要重复{{1}}。 (在类定义之外定义类成员时使用它。)

答案 1 :(得分:-1)

将其声明为学生班级中的朋友函数,旁边是其他成员函数

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

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