Archived

This topic is now archived and is closed to further replies.

xjoshbx

problem with templates

Recommended Posts

lets say I have a template class with a struct as private data. One of my private member functions in the class returns a pointer of the struct type. For some reason, my function gets syntax errors all over, and it doesn''t make sense at all. What am I doing wrong? I''ve scoured over all the documentation on templates I can find and I''m totally lost!!! Here is the code:
  
//I commented out unneccessary parts

template <class TYPE>
class BinaryTree
{
	struct NODE
	{
	TYPE data;
	NODE *left;
	NODE *right;
	};

	NODE root;
	NODE* insertNode(NODE*,TYPE);
public:
	//BinaryTree();

	void insertNode(TYPE);
	//void deleteNode(TYPE);

};

template <class TYPE>
void BinaryTree<TYPE>::insertNode(TYPE data)
{
	root = insertNode(root,data);
}

template <class TYPE>
//FOUR SYNTAX ERRORS ON THIS LINE:

NODE * BinaryTree<TYPE>::insertNode(NODE *root, TYPE data)
{
	if(root=NULL)
	{
		root = new NODE;
		root->data = data;
		root->left = NULL;
		root->right = NULL;
	}
	else

	if(data > root->data)
	{ 
		if(root->right)
			insertNode(root->right,data);
		else
		{
			NODE *node = new NODE;
			node->data = data;
			node->left = NULL;
			node->right = NULL;
			root->right = node;
		}
	}
	else (data > root->data)
	{ 
		if(root->left)
			insertNode(root->left,data);
		else
		{
			NODE *node = new NODE;
			node->data = data;
			node->left = NULL;
			node->right = NULL;
			root->right = node;
		}
	}
	return(root);
}
  

Share this post


Link to post
Share on other sites
Visual Studio doesn''t support non-inline declaration of template class templated member-functions. There''s a bug report somewhere but I can''t be bothered

Move the functions declarations in the body of the class and check if it works.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Try this line instead:

    
template <class TYPE>
BinaryTree<TYPE>::NODE * BinaryTree<TYPE>::insertNode(BinaryTree<TYPE>::NODE *root, TYPE data)


[edited by - Martee on June 6, 2002 3:37:17 PM]

Share this post


Link to post
Share on other sites
clarification for you:


  
class Blah
{
public:
void NonInlineFunction();
inline void InlineFunction();
void AnotherInlineFunction()
{
}
};

void Blah::NonInlineFunction()
{
}

inline void Blah::InlineFunction()
{
}



the issue with VS6 is that non inline functions (ex: Blah::NonInlineFunction) and external inline functions (ex: Blah::InlineFunction) are not supported with templates.

To do templates with VS6 I believe you must define them as an internal inline function (ex: Blah::InlineFunction)

I believe service packs or VS7 fix some of these problems. Dunno what fixes are made exactly.

-SniperBoB-

Share this post


Link to post
Share on other sites
hmm no that didn''t help at all..

are you sure there isn''t something else? am I supposed to make struct NODE template also maybe?
I tried that..
Or is something else wrong with how I''m declaring the templates?
Damn this is driving me crazy, and I''m getting desperate.
This is a school assignment too (not due for a week, but still), so I can''t just say "oh screw the templates, I''ll just change the type manually".

Also, I appreciate the help. I''m going to go back to googling for an answer for now. Thanks.

Share this post


Link to post
Share on other sites
btree.h(39) : error C2143: syntax error : missing '';'' before ''*''
btree.h(39) : error C2501: ''NODE'' : missing storage-class or type specifiers
btree.h(39) : error C2059: syntax error : '';''
btree.h(39) : error C2065: ''TYPE'' : undeclared identifier
btree.h(39) : error C2061: syntax error : identifier ''TYPE''

note: if I replace NODE* with int* as the return type, it compiles just fine.

Share this post


Link to post
Share on other sites
Since you are declaring the function outside the class, the compiler has no idea where NODE is

You need to do something like


  
template <class TYPE>
BinaryTree<TYPE>::Node* BinaryTree<TYPE>::insertNode( BinaryTree<TYPE>::Node *root


You might need typename keyword too to be fully portable.

Share this post


Link to post
Share on other sites
quote:
Original post by xjoshbx
The only problem is, I have no idea why that works.

Your NODE class/struct is private to the BinaryTree class and as such is undefined in the global (or outer) namespace. Since I''m not in the mood for a full discussion of namespaces and scope, look ''em up. Anyway, to successfully introduce NODE into the relative global namespace, it must be fully qualified (prefixed by the containing structure identifier).

Share this post


Link to post
Share on other sites