如何重载c++中模板类的操作符

How do I overload an operator for a template class in C++?

本文关键字:操作符 c++ 何重载 重载      更新时间:2023-10-16

我有一个二叉搜索树类(BST.h)和一个节点类(node .h),当我在其中存储数据类型(如整数)时,它们工作得很好。我的问题是尝试在我的BST中存储类对象,并使用对象的属性作为键。我的程序还有一个包含studententid和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;
}

你似乎是在问operator<Student s,然而Student不是一个类模板,所以你的帖子的标题令人费解。

正如其他人指出的,你的operator<几乎是正确的,除了你必须实际调用GetID(),而不是比较指针和成员函数。

这将不会工作,直到你修复GetID然而。而不是int Student::GetID();,应该是:

int GetID() const;

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

将其声明为Student类中的友元函数,紧挨着其他成员函数

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

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