delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/11/06:24:05

Date: Tue, 11 Jan 2000 09:50:19 +0200 (EET)
Message-Id: <200001110750.JAA80114@openet.freenet.hut.fi>
From: joh0 AT freenet DOT hut DOT fi (Jarno Honkanen)
To: djgpp AT delorie DOT com
Subject: Template problem
Reply-To: djgpp AT delorie DOT com


I have problems with templates. I use gcc version 2.8.1.
My simple linked list app crash always when I use templetes.
I wonder what I doing wrong. Could you tell me how I should
to do this.

Thanks

Jarno Honkanen
joh0 AT freenet DOT hut DOT fi



/// here is my linked list app ///////////////////////////

template <class T>
class      List{
public:
		List()        :root(0),   next(0) {}
		List(T *node) :root(node),next(0) {}
		~List();
	void	add_node(T *node);
	T*	get_node() const         { return root; }
	List<T>*	get_next() const { return next; }
	void	set_next(List<T> *list)  { next = list; }
private:
	void	add_node(List<T> *list);

	T	*root;
	List<T>	*next;
};

template <class T>
List<T>::~List(){
	delete root;
	delete next;
}
template <class T>
void List<T>::add_node(List<T> *list){
	if (!next)
		next = list;
	else
		next->add_node(list);
}
template <class T>
void List<T>::add_node(T *root){
	List	*list = 0;
	list = new List(root);
	add_node(list);
}

int main()
{
	List<int> *root;
	int	*data;

	root = new List<int>; // app crash here, why?

	data = new int;
	*data = 10;
	root->add_node(data);

	delete root;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019