I have a Tree class that holds pointers to Player objects.

Here is the code:

#pragma once
#include "Player.h"

class Tree
{

private:
	
public:
	Player* root;
	Tree(void);
	~Tree(void);

	void DisplayInOrder(Player* localRoot)
	{
		        if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
				}
	}

	Player* Find(const Player* key)
	{
		            Player * current = root;
            while (current != key) {
				if (key < current)
                    current = current->leftChild;
                else
                    current = current->rightChild;
                if (current == 0)
				{
				    cout << endl << "Player Not Found!" << endl << endl;
                    return 0;

				}
            }
			cout << endl << endl << "Found Player! ";
			current->Display();
			cout << endl << endl;
            return current;
	}

	void Insert(Player* insertedPlayer)
	{

		if(root == 0)
	    {
		     root = insertedPlayer;
	     }
	     else
	     {
		     Player * current = root;
		     Player * parent;
		     while(true)
		     {
			     parent = current;
				 if(insertedPlayer < current)
			      {
				     current = current->leftChild;
					 if(current == 0)
					     {
						     parent->leftChild = insertedPlayer;
						     return;
					     }
				  }
			      else
			      {
			          current = current->rightChild;
				      if(current == 0)
				      {
							     parent->rightChild = insertedPlayer;
							     return;
				      }
			
			       }
		     
		     }
	     }

	}

};

The problem is, if I insert 5 pointers to Player objects using the Insert function, then call the displayInOrder function, passing it the root as the argument, it outputs the objects in the same order I put them in, rather than in order of their "id" data member that the Insert function takes into account using overloaded operators < and != for the Player class.

I have a working example of a Tree structure that I have literally copied and pasted (with the necessary changes) in, but it still doesn't seem to work properly...

I'm at a loss ti fugure out why!

Thanks

>if(insertedPlayer < current)
You're comparing pointers, not the "id" data member.

>if(insertedPlayer < current)
You're comparing pointers, not the "id" data member.

I have overloaded the < and != operator for the Player class, which both operators are pointers to. Does it not work that way?

Here are the overloaded operators in the Player class:

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


class Player: public GameObject
{

private:

	bool operator!=(const Player& ent)
	{
		if(this->id != ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	bool operator<(const Player& ent)
	{
		if(this->id < ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	

public:
    LinkedList* weapons;

	Player* leftChild;
	Player* rightChild; 

	Player(int id);
	~Player(void);

	void Display()
	{
		cout << "Player ID: " << this->id << endl;
	}
};

You need to deference you pointers

You need to deference you pointers

That throws up a build error:

Error 2 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'Player' (or there is no acceptable conversion) c:\users\james\documents\visual studio 2008\projects\assignment 2 (aip)\assignment 2 (aip)\tree.h 58

Can you show your implementation of the overloaded operators you mentioned?

Can you show your implementation of the overloaded operators you mentioned?

I already did but here it is again:

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


class Player: public GameObject
{

private:

	bool operator!=(const Player& ent)
	{
		if(this->id != ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	bool operator<(const Player& ent)
	{
		if(this->id < ent.id)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	

public:
    LinkedList* weapons;

	Player* leftChild;
	Player* rightChild; 

	Player(int id);
	~Player(void);

	void Display()
	{
		cout << "Player ID: " << this->id << endl;
	}
};

Maybe you're comparing a GameObject with a Player, but I don't think so...

Have you deferenced BOTH pointers? (current AND insertedPlayer)

Note that you put overrided operators PRIVATE, so this is hidden... Try to put them PUBLIC.

Maybe you're comparing a GameObject with a Player, but I don't think so...

Have you deferenced BOTH pointers? (current AND insertedPlayer)

Note that you put overrided operators PRIVATE, so this is hidden... Try to put them PUBLIC.

You nailed it, I needed to dereference both operands.

I think it's because the overloaded operator is expecting a dereferenced right operand.

I don't think it matters if the overloaded operators are private since they are technically a member function, but I don't know lol

Thanks!

James

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.