简体   繁体   English

如何在C ++中重载模板类的运算符?

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

I have a binary search tree class (BST.h) and a node class (Node.h) of which works fine when I store data types such as integers in it. 我有一个二进制搜索树类(BST.h)和一个节点类(Node.h),当我在其中存储数据类型(例如整数)时,该类的工作正常。 My problem is trying store class objects in my BST and use an attribute from the object as the key. 我的问题是尝试在BST中存储类对象,并使用该对象的属性作为键。 My program also has a student class which contains studentID and studentName. 我的程序还有一个学生类,其中包含studentID和studentName。 How would I write an operator overload in my student class so every time my BST preforms operation on nodes, it will overload to the student.getID(), instead of operating on the object itself. 我将如何在学生类中编写运算符重载,因此每次我的BST在节点上执行操作时,它将重载到student.getID(),而不是对对象本身进行操作。 I have the rough idea of what the overload function should look like but i don't know where it should go or if its coded correctly anyway. 我对重载函数应该是什么样子有一个大概的想法,但是我不知道它应该去哪里或者它的编码是否正确。

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

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

You seem to be asking about operator< taking Student s , however Student is not a class template, so the title of your post is baffling. 您似乎在问operator<Student ,但是Student不是类模板,因此您的帖子标题令人困惑。

As someone else pointed out, your operator< is almost correct, except you have to actually call GetID() instead of comparing pointers to member functions. 正如其他人指出的那样,您的operator<几乎是正确的,除了您必须实际调用GetID()而不是比较指向成员函数的指针。

This won't work yet until you fix GetID however. 但是,在您修复GetID之前,这将无法工作。 Instead of int Student::GetID(); 代替int Student::GetID(); it should be: 它应该是:

int GetID() const;

The const means that it can be called on objects passed by const reference, as you have in your operator< implementation. const意味着它可以在const引用传递的对象上调用,就像在operator<实现中一样。 And you don't repeat the Student:: when declaring functions inside the class. 在类中声明函数时,您无需重复Student:: (You use it when defining class members outside of the class definition). (在类定义之外定义类成员时使用它)。

Declare it as a friend function within your Student class, next to the rest of your member functions 在您的Student类中,将其声明为好友函数,在其余成员函数旁边

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

Your implementation is correct, it should go outside your Student class within the same header file. 您的实现是正确的,它应该在同一头文件中的Student类之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM