LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2008, 08:24 AM   #1
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
Unhappy Compilation problem in c++ template code :(


Hi
Below is a very trivial code that i wrote to learn templates in C++.
However it is giving a compilation error that i am unable to resolve even after banging my head for more than 2 hours. I have googled it as well but no help.

I request you to just compile this code once, its giving an error i am unable to resolve.

I am also marking the line where it gives error ( i am using GCC 4.2.3 )
and the error is "error: expected constructor, destructor, or type conversion before '*' token"
Code:
//************************STANDARD LIBRARY HEADERS*************************************
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstring>
#include<map>
#include<bitset>
#include<functional>
#include<deque>
#include<cassert>
#include<ctime>
#include<cctype>
#include<cstdlib>
#include<cstddef>
#include<algorithm>
#include<iterator>
#include<utility>
#include<cmath>
#include<vector>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<numeric>
#include<cstdio>
#include<iomanip>
#include<complex>
//*************************************************************



//*************************SYSTEM HEADERS*****************************
#include<unistd.h>
#include<pthread.h>
#include<signal.h>
#include<dirent.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/wait.h>
//********************************************************************
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef list<int> LI;
typedef list<double> LD;
typedef deque<int> DI;
typedef deque<string> DS;
typedef deque<DS> DDS;
typedef stack<int> SI;
typedef stack<string> SS;
typedef map<int,string> MIS;
typedef map<string,int> MSI;
typedef map<int,int> MII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;
typedef pair<int,int> PII;
typedef set<int> STI;
typedef multiset<int>MSTI;
typedef long long LL;
#define PI 3.14159265358979323846264338327950288
#define I(x) x::iterator
template<typename T>
class BinarySearchTree
{
	public:
		BinarySearchTree();
		~BinarySearchTree();
		void insert(const T&);
		void inOrderTraversal();
		void clear();
	private:
		class node
		{
			public:
				node(const T& _key)
				{
					key=_key;
					left=0;
					right=0;
					parent=0;
				}

			private:
			T key;
			node* left;
			node* right;
			node* parent;
			friend class BinarySearchTree;
		};
		void clear(node*);
		node* createNode(const T&);
		void inOrderTraversal(const node*);
		node* m_root;
		bool m_isFlag; // To counter the problem of equals
};
template<typename T>
BinarySearchTree<T>::BinarySearchTree():m_root(0),m_isFlag(true)
{
}
template<typename T>
BinarySearchTree<T>::~BinarySearchTree()
{
	clear(m_root);
	m_root=0;
}
template<typename T>
void BinarySearchTree<T>::insert(const T& _key)
{
	node* l_new_node=createNode(_key);
	node* current=m_root;
	node* temp=0;
	while(current!=0)
	{
		temp=current;
		if(current->key == _key)
		{
			if(m_isFlag)
			{
				current=current->left;
				m_isFlag=false;
			}
			else
			{
				current=current->right;
				m_isFlag=true;
			}
		}
		else if(current->key < _key)
		{
			current=current->right;
		}
		else
		{
			current=current->left;
		}
	}
	l_new_node->parent=temp;
	if(!temp)
	{
		m_root=l_new_node;
	}
	else
	{
		if(temp->key < _key)
		{
			temp->right=l_new_node;
		}
		else
		{
			temp->left=l_new_node;
		}
	}
}
template<typename T>
BinarySearchTree<T>::node* BinarySearchTree<T>::createNode(const T& _key) /*******************This part is giving the error****************/
{
	return new node(_key);
}
template<typename T>
void BinarySearchTree<T>::inOrderTraversal()
{
	inOrderTraversal(m_root);
}
template<typename T>
void BinarySearchTree<T>::inOrderTraversal(const node* head)
{
	if(head==0)
	{
		return;
	}
	inOrderTraversal(head->left);
	cout<<head->key<<endl;
	inOrderTraversal(head->right);
}
template<typename T>
void BinarySearchTree<T>::clear(node* head)
{
	if(head==0)
	{
		return;
	}
	clear(head->left);
	clear(head->right);
	delete head;
}
template<typename T>
void BinarySearchTree<T>::clear()
{
	clear(m_root);
}
int main()
{
	BinarySearchTree<int> tree1;
	tree1.insert(5);
	tree1.insert(4);
	tree1.insert(17);
	tree1.insert(10);
	tree1.insert(7);
	tree1.insert(6);
	tree1.insert(2);
	tree1.insert(0);
	tree1.insert(9);
	tree1.inOrderTraversal();
	return 0;
}
Thanks for your patience
Regards
lali
 
Old 10-12-2008, 09:23 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
template<typename T>
typename BinarySearchTree<T>::node* BinarySearchTree<T>::createNode(const T& _key) 
{
	return new node(_key);
}
I have lost count of the number of times I have posted the following but... the type is dependent on the template parameter prefix it with typename.
I have to ask why you have this function in the first place and also why you include so many headers.
 
Old 10-13-2008, 06:44 PM   #3
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Original Poster
Rep: Reputation: 16
Hey thanks a lot for your help.
Quote:
I have to ask why you have this function in the first place
So that i can insert a new node. Moreover, I have included so many headers just like that, they don't have any overhead, do they ?


Thanks again for your help
Regards
lali
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
from django.template import Template, gives output firedancer Linux - Newbie 0 11-30-2007 02:08 PM
D-ITG compilation error - me or the code? baldy3105 Linux - Software 2 12-08-2006 04:47 AM
Compilation problem overloading operator () to template class constructor. ianix Programming 1 04-19-2006 01:16 PM
Compilation error while mixing C & C++ code. mghiya Programming 3 04-11-2006 09:45 AM
compilation error - code 2 jhon Programming 2 08-31-2004 01:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration