Trouble tracing down compile errors (C++)

visionviper

[H]ard|Gawd
Joined
Jul 24, 2007
Messages
1,222
Here is my code

Here is the list of errors:
Error 1 error C2244: 'BinarySearchTree<Comparable>::findMin' : unable to match function definition to an existing declaration g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 95
Error 2 error C2143: syntax error : missing ';' before '*' g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 212
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 212
Error 4 error C2065: 'Comparable' : undeclared identifier g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 212
Error 5 error C2143: syntax error : missing ';' before '*' g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 227
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 227
Error 7 error C2086: 'int BinaryNode' : redefinition g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 227
Error 8 error C2143: syntax error : missing ';' before '*' g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 276
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 276
Error 10 error C2086: 'int BinaryNode' : redefinition g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h 276

The first error is the one that has me the most, and the other errors I am just finding puzzling. Suggestions? Thanks in advance for any help.
 
I solved the first one, I had forgotten a const. Now I am stuck on the remaining 9 :-/
 
Sounds like you're not including some necessary headers and you're using a type that the compiler doesn't know about.

Some code would be nice.
 
Unfortunately, the compiler has to react to the source that you give it, as it implements the language specification. Your intention, particularly when you make an error, isn't always apparent to the compiler.

In this situation, the compiler reports that it found what it thought should be a statement but was really the beginning of a function definition. That function definition contains a type that the compile can't identify, so it assumes that it's not a type and is really something else--something that should've been followed by a semicolon to make a valid constrct.

The declaration in question is here:
Code:
template <typename Comparable>
BinaryNode* BinarySearchTree<Comparable>::findMin ( BinaryNode* t ) const
{
	if ( t == NULL )
	{
		return NULL;
	}
	else if ( t->left == NULL )
	{
		return t;
	}

	return findMin( t->left );
}

I think you're intending to return a pointer to a BinaryNode, but the thing is that BinaryNode is a class that's internal to the BinarySearchTree class that you're implementing and not a global class--as you've implied with this definition. The compiler doesn't traverse all the name spaces and scopes that it knows to try and find something that could've possibly matched; instead, it asumes you meant to declare something else, tries to parse it that way, and fails. That's why the error message isn't so leading. With a bit of experience, you'll start to recognize these diagnostics and remember the things to check to remedy them.

For your specific bit of code, I think you want to make the declaration of the function this way:

Code:
template <typename Comparable>
BinarySearchTree::BinaryNode* BinarySearchTree<Comparable>::findMin ( BinaryNode* t ) const
{
	if ( t == NULL )
	{
		return NULL;
	}
	else if ( t->left == NULL )
	{
		return t;
	}

	return findMin( t->left );
}

so that the type is scoped, and the compiler searches the right name space scope for the type definition. This is the same problem on your findMax() and clone() members.

Hope that helps; let me know if you've got any other questions.
 
Hope that helps; let me know if you've got any other questions.

When I try what you suggested I get a new set of errors/warning, many of them the same.

Code:
Warning	1	warning C4346: 'BinarySearchTree<Comparable>::BinaryNode' : dependent name is not a type	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	264	
Error	2	error C2143: syntax error : missing ';' before '*'	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	264	
Error	3	error C2875: using-declaration causes a multiple declaration of 'BinaryNode'	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	264	
Error	4	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	264	
Warning	5	warning C4183: 'clone': missing return type; assumed to be a member function returning 'int'	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	264	
Error	6	error C2244: 'BinarySearchTree<Comparable>::clone' : unable to match function definition to an existing declaration	g:\programming\cpts 223\program 2\bst\bst\binarysearchtree.h	536

Also, I am compiling in VS2005 if anyone needs to know.

I fixed the findMin and findMax functions by changing how I want them to work - so right now my only outstanding issue is with my clone function. Worst yet, this is the one I need the most (for the operator overloading).
 
I've been doing some more work. I decided to go ahead an create a separate template class for BinaryNode. It seems the only remaining problem in my application is now the clone function/overloaded operator in BinarySearchTree.

When I clone the tree I somehow end up with a tree filled with bad pointers mixed in with the real nodes.

Here's my latest source code:
BinarySearchTree.h
BinaryNode.h

I apologize for my lack of commenting, but I have a bad habit of commenting last (instead of first).

EDIT: It's not really critical that I fix it - though it will bug the crap out of me. I have a work around that while it doesn't meet the requirements the end result is the same.
 
Sorry; I guess you've moved on, but here's the corrected declaration. You can also solve the problem by moving the declaration inline; that's always simpler.

Code:
template <typename Comparable>
typename BinarySearchTree<typename Comparable>::BinaryNode*
BinarySearchTree<typename Comparable>::findMin ( BinaryNode* t ) const
{
	if ( t == NULL )
	{
		return NULL;
	}
	else if ( t->left == NULL )
	{
		return t;
	}

	return findMin( t->left );
}
 
Back
Top