Skip to main content
GameDev.net gamedev.net
🔒 Locked

More tree errors.

Started by Daishim Apr 25, 2004 at 5:36 PM 2 replies 400+ views
Original Post
Daishim
Daishim
I''m still working on a tree and I''ve come across some more strange errors. This is getting really frustrating, I can''t use another compiler and these messages are rather useless. Error:
g++ -Wall -c tree.cpp
tree.cpp: In function `std::ostream& operator<<(std::ostream&, const Tree&)'':
tree.cpp:73: no matching function for call to `Tree:rint(Tree::Node* const*,
   int) const''
tree.cpp:62: candidates are: void Tree:rint(Tree::Node*, int)
make: *** [tree.o] Error 1
 
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 *trav(const int foo, Node *& par) const;

	void axe(Node * curr);

	public:
		Node * root;

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

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

		void print(Node *, 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(const int foo, 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(const int foo) const
{
	Node * par = NULL;
	return trav(foo, par);
}

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) << curr->data << endl;
		print(curr->left, level + 1);
	}
}

ostream &operator<<(ostream &ost, const 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 = trav(foo, par);

	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 able to help? I''d really appreciate it, I''m going nuts trying to figure out all these ambiguous messages.

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.
SiCrane
SiCrane
Did you try changing your print() function to accept a const Node pointer?
Aprosenf
Aprosenf
Change

void print(Node *, int)

to

void print(const Node *, int) const

and change

t.print(&t.root, 1);

to

t.print(t.root, 1);

since t.root is already a pointer.
Daishim
Daishim
I''ve tried that, and by doing that the compiler just runs endlessly... this is why I hate g++.



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.

Topic Locked

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

Sign in to reply to this topic.