VS2005 HELP - error LNK2019 - unresolved external symbol

also fatal error LNK1120 - 1 unresolved externals

Page 1 of 1

3 Replies - 4770 Views - Last Post: 25 November 2008 - 03:43 PM Rate Topic: -----

#1 AlexH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-November 08

VS2005 HELP - error LNK2019 - unresolved external symbol

Posted 25 November 2008 - 03:06 PM

Hi again,

I am using VS2005. I am new to programming so I don't know what these errors mean. I think it has something to do with my swap method. My assignment is to swap all of the left and right subtrees in a binary search tree. The swap method calls the swapSubtrees method.

The errors are:
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall binaryTreeType<int>::swap(void)" (?swap@?$binaryTreeType@H@@QAEXXZ) referenced in function _wmain DS Week 3 Page 716 Exercise 3.obj


Error 2 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\Alex\My Documents\Visual Studio 2005\Projects\DS Week 3 Page 716 Exercise 3\Debug\DS Week 3 Page 716 Exercise 3.exe

Here is the code.

main function
// DS Week 3 Page 716 Exercise 3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "binaryTree.h"
#include "binarySearchTree.h"
#include <iostream>
using namespace std;




int _tmain(int argc, _TCHAR* argv[])
{
	bSearchTreeType<int> * bstt = new bSearchTreeType<int>();

	int i = 0;
	bool keepGoing = true;
	
	cout << "We are going to build a binary tree now." << endl;

	while(keepGoing)
	{
		
		cout << "Please enter a number." << endl;
		cin >> i;
		cout << "you entered " << i << endl;
		bstt->insert(i);
		cout << "Do you want to continue?  Enter 1 for yes and 0 for no." << endl;
		cin >> keepGoing;

	}
	cout << endl;
	cout << "before swapping" << endl;
	// print tree
	bstt->inorderTraversal();

	cout << endl;
	bstt->swap();
	cout << "after swapping" << endl;
	// print tree
	bstt->inorderTraversal();
	
	
}




binaryTreeType class
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree

#include <iostream>

using namespace std;

//Definition of the node
template<class elemType>
struct nodeType
	{
	elemType			   info;
	nodeType<elemType>  *llink;
	nodeType<elemType>  *rlink;
	};

//Definition of the class
template <class elemType>
class binaryTreeType
	{
	public:
		const binaryTreeType<elemType>& operator=
			(const binaryTreeType<elemType>&); 
		//Overload the assignment operator.
		bool isEmpty();
		//Function to determine if the binary tree is empty.
		//Postcondition: Returns true if the binary tree is empty;
		//			   otherwise, returns false.
		void inorderTraversal();
		//Function to do an inorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the inorder sequence.
		void preorderTraversal();
		//Function to do a preorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the preorder sequence.
		void postorderTraversal();
		//Function to do a postorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the postorder sequence.

		int treeHeight();
		//Function to deteremine the height of the binary tree.
		//Postcondition: The height of the binary tree is returned.
		int treeNodeCount();
		//Function to determine the number of nodes in the 
		//binary tree.
		//Postcondition: The number of nodes in the binary tree
		//			   is returned.
		int treeLeavesCount();
		//Function to determine the number of leaves in the 
		//binary tree.
		//Postcondition: The number of leaves in the binary tree
		//			   is returned.
		void destroyTree();
		//Deallocates the memory space occupied by the binary tree.
		//Postcondition: root = NULL;

		binaryTreeType(const binaryTreeType<elemType>& otherTree); 
		//copy constructor

		binaryTreeType();   
		//default constructor

		~binaryTreeType();   
		//destructor

		void swapSubTrees(nodeType<elemType> * p);

		void swap();

	public:
		nodeType<elemType> *root;

	private:
		void copyTree(nodeType<elemType>* &copiedTreeRoot,
			nodeType<elemType>* otherTreeRoot);
		//Function to make a copy of the binary tree to 
		//which otherTreeRoot points. 
		//Postcondition: The pointer copiedTreeRoot points to
		//			   the root of the copied binary tree.

		void destroy(nodeType<elemType>* &p);
		//Function to destroy the binary tree to which p points. 
		//Postcondition: The nodes of the binary tree to which
		//			   p points are deallocated.
		//			   p = NULL.

		void inorder(nodeType<elemType> *p);
		//Function to do an inorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the inorder sequence.
		void preorder(nodeType<elemType> *p);
		//Function to do a preorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the preorder sequence.
		void postorder(nodeType<elemType> *p);
		//Function to do a postorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the postorder sequence.

		int height(nodeType<elemType> *p);
		//Function to determine the height of the binary tree
		//to which p points. 
		//Postcondition: The height of the binary tree to which p
		//			   points is returned.

		int max(int x, int y);
		//Function to determine the larger of x and y.
		//Postcondition: The larger of x and y is returned.

		int nodeCount(nodeType<elemType> *p);
		//Function to determine the number of nodes in the binary 
		//tree to which p points. 
		//Postcondition: The number of nodes in the binary tree
		//			   to which p points is returned.

		int leavesCount(nodeType<elemType> *p);
		//Function to determine the number of leaves in the binary 
		//tree to which p points.
		//Postcondition: The number of nodes in the binary tree
		//			   to which p points is returned.
	};

//Definition of member functions

template<class elemType>
binaryTreeType<elemType>::binaryTreeType()
	{
	root = NULL;
	}

template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
	{
	return (root == NULL);
	}

template<class elemType>
void binaryTreeType<elemType>::inorderTraversal()
	{
	inorder(root);
	}

template<class elemType>
void binaryTreeType<elemType>::preorderTraversal()
	{
	preorder(root);
	}

template<class elemType>
void binaryTreeType<elemType>::postorderTraversal()
	{
	postorder(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeHeight()
	{
	return height(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeNodeCount()
	{
	return nodeCount(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeLeavesCount()
	{
	return leavesCount(root);
	}

template <class elemType>
void  binaryTreeType<elemType>::copyTree
(nodeType<elemType>* &copiedTreeRoot,
 nodeType<elemType>* otherTreeRoot)
	{
	if(otherTreeRoot == NULL)
		copiedTreeRoot = NULL;
	else
		{
		copiedTreeRoot = new nodeType<elemType>;
		copiedTreeRoot->info = otherTreeRoot->info;
		copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
		copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
		}
	} //end copyTree

template<class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		inorder(p->llink);
		cout<<p->info<<" ";
		inorder(p->rlink);
		}
	}

template<class elemType>
void binaryTreeType<elemType>::preorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		cout<<p->info<<" ";
		preorder(p->llink);
		preorder(p->rlink);
		}
	}

template<class elemType>
void binaryTreeType<elemType>::postorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		postorder(p->llink);
		postorder(p->rlink);
		cout<<p->info<<" ";
		}		
	}

//Overload the assignment operator
template<class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::
operator=(const binaryTreeType<elemType>& otherTree)
	{ 

	if(this != &otherTree) //avoid self-copy
		{
		if(root != NULL)  //if the binary tree is not empty, 
			//destroy the binary tree
			destroy(root);

		if(otherTree.root == NULL) //otherTree is empty
			root = NULL;
		else
			copyTree(root, otherTree.root);
		}//end else

	return *this; 
	}

template <class elemType>
void  binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
	{
	if(p != NULL)
		{
		destroy(p->llink);
		destroy(p->rlink);
		delete p;
		p = NULL;
		}
	}

template <class elemType>
void  binaryTreeType<elemType>::destroyTree()
	{
	destroy(root);
	}

//copy constructor
template <class elemType>
binaryTreeType<elemType>::binaryTreeType
(const binaryTreeType<elemType>& otherTree)
	{
	if(otherTree.root == NULL) //otherTree is empty
		root = NULL;
	else
		copyTree(root, otherTree.root);
	}

template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
	{
	destroy(root);
	}

template<class elemType>
int binaryTreeType<elemType>::height(nodeType<elemType> *p)
	{
	if(p == NULL)
		return 0;
	else
		return 1 + max(height(p->llink), height(p->rlink));
	}

template<class elemType>
int binaryTreeType<elemType>::max(int x, int y)
	{
	if(x >= y)
		return x;
	else
		return y;
	}

template<class elemType>
int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p)
	{
	cout<<"Write the definition of the function nodeCount"
		<<endl;

	return 0;
	}

template<class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p)
	{
	cout<<"Write the definition of the function leavesCount"
		<<endl;

	return 0;
	}
template<class elemType>
void swapSubTrees(nodeType<elemType> * p)
	{

	if (p != NULL)
		{

		// if node has 2 children, swap them
		if (p->llink != NULL && p->rlink != NULL)
			{

			nodeType<elemType> * temp = p->llink;
			p->llink = p->rlink;
			p->rlink = temp;
			delete temp;
			}

		// has left child but no right child
		// go down the left subtree
		if (p->llink != NULL && p->rlink == NULL)
			{
			swapSubTrees(p->llink);
			}

		// has right child but no left child
		// go down the right subtree
		if (p->rlink != NULL && p->llink == NULL)
			{
			swapSubTrees(p->rlink);
			}

		}

	else
		{
		return;
		}
	}
template<class elemType>
void swap()
	{
	swapSubTrees(root);
	}

#endif


bSearchTreeType class
//Header File Binary Search Tree

#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include <cassert>
#include "binaryTree.h"

using namespace std;

template<class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
	bool search(const elemType& searchItem);
	  //Function to determine if searchItem is in the binary 
	  //search tree.
	  //Postcondition: Returns true if searchItem is found in the 
	  //			 binary search tree; otherwise, returns false.

	void insert(const elemType& insertItem);
	  //Function to insert insertItem in the binary search tree.
	  //Postcondition: If no node in the binary search tree has 
	  //		   the same info as insertItem, a node with the 
	  //		   info insertItem is created and inserted in the
	  //binary search tree.

	void deleteNode(const elemType& deleteItem);
	  //Function to delete deleteItem from the binary search tree 
	  //Postcondition: If a node with the same info as deleteItem 
	  //			   is found, it is deleted from the binary 
	  //			   search tree.



private:
	void deleteFromTree(nodeType<elemType>* &p);
	  //Function to delete the node, to which p points, from the 
	  //binary search tree.
	  //Postcondition: The node to which p points is deleted
	  //			   from the binary search tree.


	
	};


template<class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem)
{
	nodeType<elemType> *current;
	bool found = false;

	if(root == NULL)
	   cerr<<"Cannot search the empty tree."<<endl;
	else
	{ 
	   current = root;

	   while(current != NULL && !found)
	   {
			 if(current->info == searchItem)
				found = true;
			  else
				  if(current->info > searchItem)
					 current = current->llink;
				  else
					 current = current->rlink;
	   }//end while
	}//end else

	return found;
}//end search

template<class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
	nodeType<elemType> *current;  //pointer to traverse the tree
	nodeType<elemType> *trailCurrent; //pointer behind current
	nodeType<elemType> *newNode;  //pointer to create the node

	newNode = new nodeType<elemType>;
	assert(newNode != NULL);
	newNode->info = insertItem;
	newNode->llink = NULL;
	newNode->rlink = NULL;

	if(root == NULL)
	   root = newNode;
	else
	{
	   current = root;
 
	   while(current != NULL)
	   {
		   trailCurrent = current;

		   if(current->info == insertItem)
		   {
			  cerr<<"The insert item is already in the list -- ";
			  cerr<<"duplicates are not allowed."<<endl;
			  return;
		   }
		   else
			  if(current->info > insertItem)
				 current = current->llink;
			  else
				 current = current->rlink;
	   }//end while

	   if(trailCurrent->info > insertItem)
		  trailCurrent->llink = newNode;
	   else
		  trailCurrent->rlink = newNode;
   }
}//end insert



template<class elemType>
void bSearchTreeType<elemType>::deleteNode(const elemType& deleteItem)
{
	nodeType<elemType> *current;  //pointer to traverse the tree
	nodeType<elemType> *trailCurrent; //pointer behind current
	bool found = false;

	if(root == NULL)
		cerr<<"Cannot delete from the empty tree."<<endl;
	else
	{
		current = root;
		trailCurrent = root;

		while(current != NULL && !found)
		{
			if(current->info == deleteItem)
				found = true;
			else
			{
				trailCurrent = current;

				if(current->info > deleteItem)
					current = current->llink;
				else
					current = current->rlink;
			}
		}//end while

		if(current == NULL)
			cout<<"The delete item is not in the tree."<<endl;
		else
			if(found)
			{
				if(current == root)
					deleteFromTree(root);
				else
					if(trailCurrent->info > deleteItem)
						deleteFromTree(trailCurrent->llink);
					else
						deleteFromTree(trailCurrent->rlink);
			}//end if
	}
}//end deleteNode

template<class elemType>
void bSearchTreeType<elemType>::deleteFromTree
								 (nodeType<elemType>* &p)
{
	 nodeType<elemType> *current;	//pointer to traverse 
									 //the tree
	 nodeType<elemType> *trailCurrent;   //pointer behind current
	 nodeType<elemType> *temp;		//pointer to delete the node

	 if(p == NULL)
		cerr<<"Error: The node to be deleted is NULL."
			<<endl;
	 else if(p->llink == NULL && p->rlink == NULL)
		  {
			 temp = p;
			 p = NULL;
			 delete temp;
		  }
	 else if(p->llink == NULL)
		  {
			 temp = p;
			 p = temp->rlink;
			 delete temp;
		  }
	 else if(p->rlink == NULL)
		  {
			 temp = p;
			 p = temp->llink;
			 delete temp;
		  }
	 else
	 {
		current = p->llink;
		trailCurrent = NULL;

		while(current->rlink != NULL)
		{
			trailCurrent = current;
			current = current->rlink;
		}//end while

		p->info = current->info;

		if(trailCurrent == NULL) //current did not move; 
								 //current == p->llink; adjust p
		   p->llink = current->llink;
		else
		   trailCurrent->rlink = current->llink;
 
		delete current;
	 }//end else
}//end deleteFromTree



#endif



Is This A Good Question/Topic? 0
  • +

Replies To: VS2005 HELP - error LNK2019 - unresolved external symbol

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: VS2005 HELP - error LNK2019 - unresolved external symbol

Posted 25 November 2008 - 03:11 PM

Hopefully I can be of more help then then your last thread. That sort of link error means that your program cannot find the specified function. Whether it is defined or not or if it isn't included in your main file can all be causes of said problem.

your second error is simply the first again but said in a different manner.

Problem is here:

void swap()
    {
    swapSubTrees(root);
    }



No scope :: resoluter to say what class this definition of swap belongs to.
Was This Post Helpful? 0
  • +
  • -

#3 AlexH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-November 08

Re: VS2005 HELP - error LNK2019 - unresolved external symbol

Posted 25 November 2008 - 03:36 PM

View PostKYA, on 25 Nov, 2008 - 02:11 PM, said:

Hopefully I can be of more help then then your last thread. That sort of link error means that your program cannot find the specified function. Whether it is defined or not or if it isn't included in your main file can all be causes of said problem.

your second error is simply the first again but said in a different manner.

Problem is here:

void swap()
    {
    swapSubTrees(root);
    }



No scope :: resoluter to say what class this definition of swap belongs to.


Thank you Kya. I added scope resolution to the swap and swapSubtrees functions, and it cleared up the two errors but now I have two new ones.

error C2244: 'binaryTreeType<elemType>::swap' : unable to match function definition to an existing declaration c:\documents and settings\alex\my documents\visual studio 2005\projects\ds week 3 page 716 exercise 3\ds week 3 page 716 exercise 3\binarytree.h 363

error C2244: 'binaryTreeType<elemType>::swapSubtrees' : unable to match function definition to an existing declaration c:\documents and settings\alex\my documents\visual studio 2005\projects\ds week 3 page 716 exercise 3\ds week 3 page 716 exercise 3\binarytree.h 357

Here's the new code.

main function
// DS Week 3 Page 716 Exercise 3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "binaryTree.h"
#include "binarySearchTree.h"
#include <iostream>
using namespace std;




int _tmain(int argc, _TCHAR* argv[])
{
	bSearchTreeType<int> * bstt = new bSearchTreeType<int>();

	int i = 0;
	bool keepGoing = true;
	
	cout << "We are going to build a binary tree now." << endl;

	while(keepGoing)
	{
		
		cout << "Please enter a number." << endl;
		cin >> i;
		cout << "you entered " << i << endl;
		bstt->insert(i);
		cout << "Do you want to continue?  Enter 1 for yes and 0 for no." << endl;
		cin >> keepGoing;

	}
	cout << endl;
	cout << "before swapping" << endl;
	// print tree
	bstt->inorderTraversal();

	cout << endl;
	bstt->swap();
	cout << "after swapping" << endl;
	// print tree
	bstt->inorderTraversal();
	
	
}




binaryTreeType class
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree

#include <iostream>

using namespace std;

//Definition of the node
template<class elemType>
struct nodeType
	{
	elemType			   info;
	nodeType<elemType>  *llink;
	nodeType<elemType>  *rlink;
	};

//Definition of the class
template <class elemType>
class binaryTreeType
	{
	public:
		const binaryTreeType<elemType>& operator=
			(const binaryTreeType<elemType>&); 
		//Overload the assignment operator.
		bool isEmpty();
		//Function to determine if the binary tree is empty.
		//Postcondition: Returns true if the binary tree is empty;
		//			   otherwise, returns false.
		void inorderTraversal();
		//Function to do an inorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the inorder sequence.
		void preorderTraversal();
		//Function to do a preorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the preorder sequence.
		void postorderTraversal();
		//Function to do a postorder traversal of the binary tree.
		//Postcondition: The nodes of the binary tree are output
		//			   in the postorder sequence.

		int treeHeight();
		//Function to deteremine the height of the binary tree.
		//Postcondition: The height of the binary tree is returned.
		int treeNodeCount();
		//Function to determine the number of nodes in the 
		//binary tree.
		//Postcondition: The number of nodes in the binary tree
		//			   is returned.
		int treeLeavesCount();
		//Function to determine the number of leaves in the 
		//binary tree.
		//Postcondition: The number of leaves in the binary tree
		//			   is returned.
		void destroyTree();
		//Deallocates the memory space occupied by the binary tree.
		//Postcondition: root = NULL;

		binaryTreeType(const binaryTreeType<elemType>& otherTree); 
		//copy constructor

		binaryTreeType();   
		//default constructor

		~binaryTreeType();   
		//destructor

		void swapSubtrees(nodeType<elemType> * p);

		void swap();

	public:
		nodeType<elemType> *root;

	private:
		void copyTree(nodeType<elemType>* &copiedTreeRoot,
			nodeType<elemType>* otherTreeRoot);
		//Function to make a copy of the binary tree to 
		//which otherTreeRoot points. 
		//Postcondition: The pointer copiedTreeRoot points to
		//			   the root of the copied binary tree.

		void destroy(nodeType<elemType>* &p);
		//Function to destroy the binary tree to which p points. 
		//Postcondition: The nodes of the binary tree to which
		//			   p points are deallocated.
		//			   p = NULL.

		void inorder(nodeType<elemType> *p);
		//Function to do an inorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the inorder sequence.
		void preorder(nodeType<elemType> *p);
		//Function to do a preorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the preorder sequence.
		void postorder(nodeType<elemType> *p);
		//Function to do a postorder traversal of the binary
		//tree to which p points.  
		//Postcondition: The nodes of the binary tree to which p
		//			   points are output in the postorder sequence.

		int height(nodeType<elemType> *p);
		//Function to determine the height of the binary tree
		//to which p points. 
		//Postcondition: The height of the binary tree to which p
		//			   points is returned.

		int max(int x, int y);
		//Function to determine the larger of x and y.
		//Postcondition: The larger of x and y is returned.

		int nodeCount(nodeType<elemType> *p);
		//Function to determine the number of nodes in the binary 
		//tree to which p points. 
		//Postcondition: The number of nodes in the binary tree
		//			   to which p points is returned.

		int leavesCount(nodeType<elemType> *p);
		//Function to determine the number of leaves in the binary 
		//tree to which p points.
		//Postcondition: The number of nodes in the binary tree
		//			   to which p points is returned.
	};

//Definition of member functions

template<class elemType>
binaryTreeType<elemType>::binaryTreeType()
	{
	root = NULL;
	}

template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
	{
	return (root == NULL);
	}

template<class elemType>
void binaryTreeType<elemType>::inorderTraversal()
	{
	inorder(root);
	}

template<class elemType>
void binaryTreeType<elemType>::preorderTraversal()
	{
	preorder(root);
	}

template<class elemType>
void binaryTreeType<elemType>::postorderTraversal()
	{
	postorder(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeHeight()
	{
	return height(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeNodeCount()
	{
	return nodeCount(root);
	}

template<class elemType>
int binaryTreeType<elemType>::treeLeavesCount()
	{
	return leavesCount(root);
	}

template <class elemType>
void  binaryTreeType<elemType>::copyTree
(nodeType<elemType>* &copiedTreeRoot,
 nodeType<elemType>* otherTreeRoot)
	{
	if(otherTreeRoot == NULL)
		copiedTreeRoot = NULL;
	else
		{
		copiedTreeRoot = new nodeType<elemType>;
		copiedTreeRoot->info = otherTreeRoot->info;
		copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
		copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
		}
	} //end copyTree

template<class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		inorder(p->llink);
		cout<<p->info<<" ";
		inorder(p->rlink);
		}
	}

template<class elemType>
void binaryTreeType<elemType>::preorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		cout<<p->info<<" ";
		preorder(p->llink);
		preorder(p->rlink);
		}
	}

template<class elemType>
void binaryTreeType<elemType>::postorder(nodeType<elemType> *p)
	{
	if(p != NULL)
		{
		postorder(p->llink);
		postorder(p->rlink);
		cout<<p->info<<" ";
		}		
	}

//Overload the assignment operator
template<class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::
operator=(const binaryTreeType<elemType>& otherTree)
	{ 

	if(this != &otherTree) //avoid self-copy
		{
		if(root != NULL)  //if the binary tree is not empty, 
			//destroy the binary tree
			destroy(root);

		if(otherTree.root == NULL) //otherTree is empty
			root = NULL;
		else
			copyTree(root, otherTree.root);
		}//end else

	return *this; 
	}

template <class elemType>
void  binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
	{
	if(p != NULL)
		{
		destroy(p->llink);
		destroy(p->rlink);
		delete p;
		p = NULL;
		}
	}

template <class elemType>
void  binaryTreeType<elemType>::destroyTree()
	{
	destroy(root);
	}

//copy constructor
template <class elemType>
binaryTreeType<elemType>::binaryTreeType
(const binaryTreeType<elemType>& otherTree)
	{
	if(otherTree.root == NULL) //otherTree is empty
		root = NULL;
	else
		copyTree(root, otherTree.root);
	}

template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
	{
	destroy(root);
	}

template<class elemType>
int binaryTreeType<elemType>::height(nodeType<elemType> *p)
	{
	if(p == NULL)
		return 0;
	else
		return 1 + max(height(p->llink), height(p->rlink));
	}

template<class elemType>
int binaryTreeType<elemType>::max(int x, int y)
	{
	if(x >= y)
		return x;
	else
		return y;
	}

template<class elemType>
int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p)
	{
	cout<<"Write the definition of the function nodeCount"
		<<endl;

	return 0;
	}

template<class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p)
	{
	cout<<"Write the definition of the function leavesCount"
		<<endl;

	return 0;
	}

template<class elemType>
void binaryTreeType<elemType>::swapSubtrees(nodeType<elemType> * p)
	{

	if (p != NULL)
		{

		// if node has 2 children, swap them
		if (p->llink != NULL && p->rlink != NULL)
			{

			nodeType<elemType> * temp = p->llink;
			p->llink = p->rlink;
			p->rlink = temp;
			delete temp;
			}

		// has left child but no right child
		// go down the left subtree
		if (p->llink != NULL && p->rlink == NULL)
			{
			swapSubtrees(p->llink);
			}

		// has right child but no left child
		// go down the right subtree
		if (p->rlink != NULL && p->llink == NULL)
			{
			swapSubtrees(p->rlink);
			}

		}

	else
		{
		return;
		}
	}

template <class elemType>
void binaryTreeType::swap()
	{
	binaryTreeType::swapSubtrees(root);
	}

#endif



bSearchTreeType class
//Header File Binary Search Tree

#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include <cassert>
#include "binaryTree.h"

using namespace std;

template<class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
	bool search(const elemType& searchItem);
	  //Function to determine if searchItem is in the binary 
	  //search tree.
	  //Postcondition: Returns true if searchItem is found in the 
	  //			 binary search tree; otherwise, returns false.

	void insert(const elemType& insertItem);
	  //Function to insert insertItem in the binary search tree.
	  //Postcondition: If no node in the binary search tree has 
	  //		   the same info as insertItem, a node with the 
	  //		   info insertItem is created and inserted in the
	  //binary search tree.

	void deleteNode(const elemType& deleteItem);
	  //Function to delete deleteItem from the binary search tree 
	  //Postcondition: If a node with the same info as deleteItem 
	  //			   is found, it is deleted from the binary 
	  //			   search tree.



private:
	void deleteFromTree(nodeType<elemType>* &p);
	  //Function to delete the node, to which p points, from the 
	  //binary search tree.
	  //Postcondition: The node to which p points is deleted
	  //			   from the binary search tree.


	
	};


template<class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem)
{
	nodeType<elemType> *current;
	bool found = false;

	if(root == NULL)
	   cerr<<"Cannot search the empty tree."<<endl;
	else
	{ 
	   current = root;

	   while(current != NULL && !found)
	   {
			 if(current->info == searchItem)
				found = true;
			  else
				  if(current->info > searchItem)
					 current = current->llink;
				  else
					 current = current->rlink;
	   }//end while
	}//end else

	return found;
}//end search

template<class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
	nodeType<elemType> *current;  //pointer to traverse the tree
	nodeType<elemType> *trailCurrent; //pointer behind current
	nodeType<elemType> *newNode;  //pointer to create the node

	newNode = new nodeType<elemType>;
	assert(newNode != NULL);
	newNode->info = insertItem;
	newNode->llink = NULL;
	newNode->rlink = NULL;

	if(root == NULL)
	   root = newNode;
	else
	{
	   current = root;
 
	   while(current != NULL)
	   {
		   trailCurrent = current;

		   if(current->info == insertItem)
		   {
			  cerr<<"The insert item is already in the list -- ";
			  cerr<<"duplicates are not allowed."<<endl;
			  return;
		   }
		   else
			  if(current->info > insertItem)
				 current = current->llink;
			  else
				 current = current->rlink;
	   }//end while

	   if(trailCurrent->info > insertItem)
		  trailCurrent->llink = newNode;
	   else
		  trailCurrent->rlink = newNode;
   }
}//end insert



template<class elemType>
void bSearchTreeType<elemType>::deleteNode(const elemType& deleteItem)
{
	nodeType<elemType> *current;  //pointer to traverse the tree
	nodeType<elemType> *trailCurrent; //pointer behind current
	bool found = false;

	if(root == NULL)
		cerr<<"Cannot delete from the empty tree."<<endl;
	else
	{
		current = root;
		trailCurrent = root;

		while(current != NULL && !found)
		{
			if(current->info == deleteItem)
				found = true;
			else
			{
				trailCurrent = current;

				if(current->info > deleteItem)
					current = current->llink;
				else
					current = current->rlink;
			}
		}//end while

		if(current == NULL)
			cout<<"The delete item is not in the tree."<<endl;
		else
			if(found)
			{
				if(current == root)
					deleteFromTree(root);
				else
					if(trailCurrent->info > deleteItem)
						deleteFromTree(trailCurrent->llink);
					else
						deleteFromTree(trailCurrent->rlink);
			}//end if
	}
}//end deleteNode

template<class elemType>
void bSearchTreeType<elemType>::deleteFromTree
								 (nodeType<elemType>* &p)
{
	 nodeType<elemType> *current;	//pointer to traverse 
									 //the tree
	 nodeType<elemType> *trailCurrent;   //pointer behind current
	 nodeType<elemType> *temp;		//pointer to delete the node

	 if(p == NULL)
		cerr<<"Error: The node to be deleted is NULL."
			<<endl;
	 else if(p->llink == NULL && p->rlink == NULL)
		  {
			 temp = p;
			 p = NULL;
			 delete temp;
		  }
	 else if(p->llink == NULL)
		  {
			 temp = p;
			 p = temp->rlink;
			 delete temp;
		  }
	 else if(p->rlink == NULL)
		  {
			 temp = p;
			 p = temp->llink;
			 delete temp;
		  }
	 else
	 {
		current = p->llink;
		trailCurrent = NULL;

		while(current->rlink != NULL)
		{
			trailCurrent = current;
			current = current->rlink;
		}//end while

		p->info = current->info;

		if(trailCurrent == NULL) //current did not move; 
								 //current == p->llink; adjust p
		   p->llink = current->llink;
		else
		   trailCurrent->rlink = current->llink;
 
		delete current;
	 }//end else
}//end deleteFromTree



#endif


Was This Post Helpful? 0
  • +
  • -

#4 AlexH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-November 08

Re: VS2005 HELP - error LNK2019 - unresolved external symbol

Posted 25 November 2008 - 03:43 PM

I figured the new errors out. I used <class elemType> instead of <elemType> in the definition for one of the functions. It compiles now. Now I just have to figure out how to make the swap algorithm work correctly. Thanks

This post has been edited by AlexH: 25 November 2008 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1