0

I'm trying to implement a template class called "Tree", here is the code for the main function:

#include "Stack.h"
#include <iostream>
#include <time.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
	int gameObjects = 0;
	Tree<Player>* PlayerTree = new Tree<Player>;
	Player** players;
	players = new Player* [5];

	Player* p1 = new Player(0);
	players[0] = p1;
	Player* p2 = new Player(2);
	players[1] = p2;
	Player* p3 = new Player(4);
	players[2] = p3;
	Player* p4 = new Player(1);
	players[3] = p4;
	Player* p5 = new Player(3);
	players[4] = p5;

	PlayerTree->Insert(players[0]);
	PlayerTree->Insert(players[3]);
	PlayerTree->Insert(players[2]);
	PlayerTree->Insert(players[4]);
	PlayerTree->Insert(players[1]);


	players[0]->weapons->Insert(new Weapon(8,5));
	players[0]->weapons->Find(5)->magazine->Push(new Round(20));
	players[0]->weapons->Find(5)->magazine->Push(new Round(21));
	players[0]->weapons->Find(5)->magazine->Push(new Round(22));
	players[0]->weapons->Find(5)->magazine->Push(new Round(23));
	players[0]->weapons->Find(5)->magazine->Push(new Round(24));
	players[1]->weapons->Insert(new Weapon(8,6));
	players[1]->weapons->Find(6)->magazine->Push(new Round(25));
	players[1]->weapons->Find(6)->magazine->Push(new Round(26));
	players[1]->weapons->Find(6)->magazine->Push(new Round(27));
	players[1]->weapons->Find(6)->magazine->Push(new Round(28));
	players[1]->weapons->Find(6)->magazine->Push(new Round(29));
	players[1]->weapons->Insert(new Weapon(8,7));
	players[1]->weapons->Find(7)->magazine->Push(new Round(30));
	players[1]->weapons->Find(7)->magazine->Push(new Round(31));
	players[1]->weapons->Find(7)->magazine->Push(new Round(32));
	players[1]->weapons->Find(7)->magazine->Push(new Round(33));
	players[2]->weapons->Insert(new Weapon(8,8));
	players[2]->weapons->Find(8)->magazine->Push(new Round(34));
	players[2]->weapons->Find(8)->magazine->Push(new Round(35));
	players[2]->weapons->Find(8)->magazine->Push(new Round(36));
	players[2]->weapons->Find(8)->magazine->Push(new Round(37));
	players[2]->weapons->Find(8)->magazine->Push(new Round(38));
	players[2]->weapons->Insert(new Weapon(8,9));
	players[2]->weapons->Find(9)->magazine->Push(new Round(39));
	players[2]->weapons->Find(9)->magazine->Push(new Round(40));
	players[2]->weapons->Find(9)->magazine->Push(new Round(41));
	players[2]->weapons->Find(9)->magazine->Push(new Round(42));
	players[2]->weapons->Insert(new Weapon(8,10));
	players[2]->weapons->Find(10)->magazine->Push(new Round(43));
	players[2]->weapons->Find(10)->magazine->Push(new Round(44));
	players[2]->weapons->Find(10)->magazine->Push(new Round(45));
	players[3]->weapons->Insert(new Weapon(8,11));
	players[3]->weapons->Find(11)->magazine->Push(new Round(46));
	players[3]->weapons->Find(11)->magazine->Push(new Round(47));
	players[3]->weapons->Find(11)->magazine->Push(new Round(48));
	players[3]->weapons->Find(11)->magazine->Push(new Round(49));
	players[3]->weapons->Find(11)->magazine->Push(new Round(50));
	players[3]->weapons->Insert(new Weapon(8,12));
	players[3]->weapons->Find(12)->magazine->Push(new Round(51));
	players[3]->weapons->Find(12)->magazine->Push(new Round(52));
	players[3]->weapons->Find(12)->magazine->Push(new Round(53));
	players[3]->weapons->Find(12)->magazine->Push(new Round(54));
	players[3]->weapons->Insert(new Weapon(8,13));
	players[3]->weapons->Find(13)->magazine->Push(new Round(55));
	players[3]->weapons->Find(13)->magazine->Push(new Round(56));
	players[3]->weapons->Find(13)->magazine->Push(new Round(57));
	players[3]->weapons->Insert(new Weapon(8,14));
	players[3]->weapons->Find(14)->magazine->Push(new Round(58));
	players[3]->weapons->Find(14)->magazine->Push(new Round(59));
	players[4]->weapons->Insert(new Weapon(8,15));
	players[4]->weapons->Find(15)->magazine->Push(new Round(60));
	players[4]->weapons->Find(15)->magazine->Push(new Round(61));
	players[4]->weapons->Find(15)->magazine->Push(new Round(62));
	players[4]->weapons->Find(15)->magazine->Push(new Round(63));
	players[4]->weapons->Find(15)->magazine->Push(new Round(64));
	players[4]->weapons->Insert(new Weapon(8,16));
	players[4]->weapons->Find(16)->magazine->Push(new Round(65));
	players[4]->weapons->Find(16)->magazine->Push(new Round(66));
	players[4]->weapons->Find(16)->magazine->Push(new Round(67));
	players[4]->weapons->Find(16)->magazine->Push(new Round(68));
	players[4]->weapons->Insert(new Weapon(8,17));
	players[4]->weapons->Find(17)->magazine->Push(new Round(69));
	players[4]->weapons->Find(17)->magazine->Push(new Round(70));
	players[4]->weapons->Find(17)->magazine->Push(new Round(71));
	players[4]->weapons->Insert(new Weapon(8,18));
	players[4]->weapons->Find(18)->magazine->Push(new Round(72));
	players[4]->weapons->Find(18)->magazine->Push(new Round(73));
	players[4]->weapons->Insert(new Weapon(8,19));
	players[4]->weapons->Find(19)->magazine->Push(new Round(74));


	
	

    PlayerTree->DisplayInOrder(PlayerTree->root);
	PlayerTree->Find(players[0]);


    delete PlayerTree;




	system("PAUSE");
	return 0;
}

And the code for the Tree class:

#pragma once
#include "Player.h"

template<class T>
class Tree
{

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

	template<class T>
	void DisplayInOrder(T* localRoot)
	{
		        if (localRoot != 0) {
                DisplayInOrder(localRoot->leftChild);
				localRoot->Display();
                DisplayInOrder(localRoot->rightChild);
				}
	}

	template<class T>
	T* Find(const T* key)
	{
		            T * 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;
	}

    template<class T>
	void Insert(T* insertedPlayer)
	{

		if(root == 0)
	    {
		     root = insertedPlayer;
	     }
	     else
	     {
		     T * current = root;
		     T * 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;
				      }
			
			       }
		     
		     }
	     }

	}

};

When I try to build it, I get the errors shown in the attachment.

What the hell do they mean???

Attachments error3.jpg 361.74 KB
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by pubudu1199
0

An unresolved external error is typically telling you that you've declared but not defined a function. In this case, the constructor for your Tree class. I notice that your destructor isn't implemented either, unless you're defining them in a separate file.

0

An unresolved external error is typically telling you that you've declared but not defined a function. In this case, the constructor for your Tree class. I notice that your destructor isn't implemented either, unless you're defining them in a separate file.

Yeah, turns out the problem was I was declaring them in a .cpp file, which apparently is not allowed for template classes.

Thanks for your help!

0

hi i'm new c++ and trying to implement a binary tree, i studied your code but couldn't get it to work
can you please share the code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.