0

请看下面的代码

树.h

//Tree Data Structure.

    #pragma once
    #include <iostream>
    #include "Player.h"
    template <typename T>

    class Tree
    {
    public:
        T* root;

    Tree(void)
    {
        root = 0;
    }


    Tree::~Tree(void)
    {
    }

    //Find elements in tree (Find() function removed)


    //Display values in tree (Display() Function removed)

    //Insert data into tree
    void Insert(T * data)
    {
        T *newNode = data;

        if(this->root == 0)
        {
            root = newNode;
        }
        else
        {
            T *current = root;
            T *parent;

            while(true)
            {
                parent = current;

                if(data->id < current->id)
                {
                    current = current->leftChild;

                    if(current==0)
                    {
                        parent->leftChild = newNode;
                        return;
                    }
                }
                else
                {
                    current = current->rightChild;

                    if(current==0)
                    {
                        parent->rightChild = newNode;
                        return;
                    }
                }
            }
        }
    }
    };

播放器.h

#pragma once
#include "GameObject.h"
#include "Tree.h"

class Player:public GameObject
{
public:
    Player(void);
    ~Player(void);

    Player *rightChild;
    Player *leftChild;

    void Display();
    bool operator !=(const Player&);
    bool operator <(const Player&);

};

播放器.cpp

#include "Player.h"
#include <iostream>

Player::Player(void)
{
    leftChild = 0;
    rightChild = 0;
}


Player::~Player(void)
{
}



bool Player::operator!=(const Player& player)
{
    if(instances==NULL)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool Player::operator<(const Player& player)
{

    if(this->instances < player.instances)
    {
        return true;
    }
    else
    {
        return false;
    }
}

在这里,Tree是一个模板。该类Player是将插入到树中的类。

Tree.h, insideInsert()方法中,if(data->id < current->id)我不需要调用 Player 的<重载运算符。我怎样才能做到这一点?请帮忙!

4

1 回答 1

1

您可以取消引用您的指针,如下所示:

if (*data < *current) { ... }
于 2013-03-02T15:28:41.233 回答