请查看以下代码
tree.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;
}
}
}
}
}
};
Player.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&);
};
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
内部Insert()
方法中,我需要调用播放器的if(data->id < current->id)
重载运算符,而不是<
。我怎样才能做到这一点?请帮忙!
答案 0 :(得分:1)
您可以取消引用您的指针,如下所示:
if (*data < *current) { ... }