Skip to main content
gamedev.net
🔒 Locked

Strange tree error...

Started by Daishim Apr 24, 2004 at 6:10 PM 0 replies 300+ views
Original Post
Daishim
Daishim
I''m working on a tree class, but for some reason my code is puking up some strange errors. For some reason the compiler is just having a fit about one of my functions as if it doesn''t exist. I get the following errors:
g++ -Wall -c tree.cpp
tree.cpp: In member function `bool Tree::find(int) const'':
tree.cpp:48: no matching function for call to `Tree::trav(int&, Tree::Node*&)
   const''
tree.cpp:25: candidates are: Tree::Node* Tree::trav(int, const Tree::Node*&)
   const
tree.h: In function `std::ostream& ostream(std::ostream&, Tree&)'':
tree.h:13: `Tree::Node*Tree::root'' is private
tree.cpp:133: within this context
tree.cpp: In member function `void Tree::insert(int)'':
tree.cpp:141: no matching function for call to `Tree::trav(const int&,
   Tree::Node*&)''
tree.cpp:25: candidates are: Tree::Node* Tree::trav(int, const Tree::Node*&)
   const
 
Anyone have any idea how it gets those errors out of this: tree.h
class Tree
{
	class Node
	{
		public:
			Node * left;
			int data;
			int count;
			Node * right;
			Node(int d = 0) : left(NULL), data(d), count(0), right(NULL) {}
	};

	Node * root;
	
	Node *trav(int foo, const Node *& par) const;

	void axe(Node * curr);

	void print(Node *, int);

	public:
		Tree(void);
		~Tree(void);
	
		bool empty(void) const;
		bool find(int) const;

		void insert(const int);
		void remove(const int);

		friend ostream &operator<<(ostream &, const Tree &);
	
};
 
tree.cpp
#include <iostream>

using namespace std;

#include <iomanip>

#include "tree.h"

Tree::Tree(void)
{
	root = NULL;
}

Tree::~Tree(void)
{
	axe(root);
}

bool Tree::empty(void) const
{
	return !root;
}

Tree::Node *Tree::trav(int foo, const Node *& par) const
{
	Node * curr = root;

	while(curr && curr->data != foo)
	{
		par = curr;

		if(curr->data < foo)
		{
			curr = curr->right;
		}
		else
		{
			curr = curr->left;
		}
	}

	return curr;
}

bool Tree::find(int foo) const
{
	Node * par = NULL;
	Node * curr = trav(foo, par);

	if(!curr)
	{
		curr = new Node(foo);
	}

	if(!par)
	{
		curr = root;
	}
	else if(par->data < foo)
	{
		par->right = curr;
	}
	else
	{
		par->left = curr;
	}
}

/*void Tree::remove(const int foo)
{
	Node * par = NULL;
	Node * curr = trav(foo, par);

	if(curr)
	{
		if(curr->right && curr->left)
		{
			Node * tmp = curr;
			curr = tmp->left;
			par = tmp;

			while(curr->right)
			{
				par = curr;
				curr = curr->right;
			}

			tmp->data = curr->data;
		}

		if(curr->left)
		{
			if(!par)
			{
				root = curr->left;
			}
			else if(par->data < curr->data)
			{
				par->right = curr->left;
			}
			else
			{
				par->left = curr->left;
			}

			delete curr;
		}
	}
}*/

void Tree::axe(Node * curr)
{
	if(curr)
	{
		axe(curr->left);
		axe(curr->right);
		delete curr;
	}
}

void Tree:rint(Node * curr, int level)
{
	if(curr)
	{
		print(curr->right, level + 1);
		cout << setw(5*level) << cout << curr->data << endl;
		print(curr->left, level + 1);
	}
}

ostream &ostream(ostream &ost, Tree &t)
{
	t.print(t.root, 1);

	return ost;
}

void Tree::insert(const int foo)
{
	Node * par = NULL;
	Node * curr = trav(foo, par);

	if(curr)
	{
		curr->count++;
	}
	else
	{
		Node * tmp = new Node(foo);

		if(!par)
		{
			root = tmp;
		}
		else if(par->data < foo)
		{
			par->right = curr;
		}
		else
		{
			par->left = curr;
		}
	}
}

void Tree::remove(const int foo)
{
	Node * par = NULL;
	Node * curr = NULL;

	if(curr)
	{
		curr->count--;
	
		if(curr->count <= 0)
		{
			if(curr->left && curr->right)
			{
				Node * tmp = curr;
				par = curr;
				curr = curr->left;

				while(curr->right)
				{
					par = curr;
					curr = curr->right;
				}
				
				tmp->data = curr->data;
			}
			
			Node * tmp = (curr->left ? curr->left : curr->right);

			if(!par)
			{
				root = tmp;
			}
			else if(par->data == curr->data)
			{
				par->left = tmp;
			}
			else
			{
				par->left = tmp;
				delete curr;
			}
		}
	}
}
 
Anyone have any ideas or clues? I''m probably just overlooking something stupid, but after looking at this code for so long I just can''t see it.

I know only that which I know, but I do not know what I know.

I know only that which I know, but I do not know what I know.
Zahlman
Zahlman
1) "std::ostream& ostream(std::ostream&, Tree&)"

I think you wanted to define the operator<< rather than some function called "ostream" here. I''m surprised it let you get as far as it did with that, it looks almost as if you''re trying to add a constructor to a std:: class :/

2) "Tree::Node *Tree::trav(int foo, const Node *& par) const"

It looks to me like par is a reference to a const Node *, which is not what you want because you''re changing par and using it as an out parameter. Did you try "Node* const& par"?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.