problem with templates

Started by
9 comments, last by xjoshbx 21 years, 10 months ago
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);
}
  
Advertisement
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
maybe I''m confused about something else.. aren''t the function declarations already in the body of the class?
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]
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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-
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.
could you post your syntax errors? that would help diagnosis much.

-me
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.
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.
Wow. That works, thanks.

The only problem is, I have no idea why that works. I guess thats why I''m posting in the "For Beginners" forum.

This topic is closed to new replies.

Advertisement