   [solved]C++ template for generic linked list
  Hi, I was trying to make a generic linked list with template because I'm using linked list a lot in my project.Here's the problem I encounteredThis is what I ended up with while debugging#include "funcs/list.h"#include <iostream>using namespace std;struct testNode{int value;testNode* next;};int main(){    test.read_config("NPC.txt");    test.print();*/    testNode* Test;    Test->value = 5;    list<testNode> testlist;    return 0;}This is my test cppand this is my declarationand I commented out everything in the implementation of the class.template<class object>class list{    public:   object* head;   object* previous;   object* next;   /*int del(object *Node);   int add(object *newNode);   //list<object>* retrieve(object key);*/};It is strange because when I compile and run, I get this "has encountered an error and must be closed down error"BUT, if I just take out one of the object* the program would actually finish without any errors.I simply cannot understand what is going on here.Could anyone help me with this?If it helps, I'm using Code::Blocks as my IDE.Thanks in advance[Edited by - polarboy on June 18, 2009 10:48:56 PM]
  Use [ source ] instead of  if you want the box ;)<br><br>If you're getting "has encountered an error and must be closed down error", try running a debug build from the IDE, then when it crashes it should show you the line that is causing the crash.<br><br>Also, if you want a templated linked list, why not just use std::list?;  Where do you instantiate your Test node? I don't see a malloc() or a new statement...   ;  Ah, thanks for the tip, I thought it was  for some reason, no idea why,haha<br><br>anyways, I used cout to determine that it happens at list&lt;testNode&gt; testlist;<br><br>I looked into std::list<br>but the amount of information overwhelmed me,lol<br>I guess I'm just lazy, but it's something that I don't really have any clue of, for things as simple to implement as a linked list, I prefer to write my own implementation, because that way, i know exactly what's going &#111;n<br><br>;  If you're lazy, why don't you just use std::list? [wink]Seriously. In most cases (and ESPECIALLY with the C++ standard library) learning to use an API is going to be much easier than writing it. Also, the Standard Library codebase is very mature, and for the vast majority (if not all) cases, you can trust it not only to be safer, but more robust and faster than your own code. The only conceivable reason I could see NOT using std::list when you need a linked list is when you're learning to code your own linked list. Any other reason does not apply except in special cases. What special case do you have thrust upon you?   ;Quote:Original post by Oberon_CommandWhere do you instantiate your Test node? I don't see a malloc() or a new statement...Ah, good point, that was dumb of me   ;  Learning how standard containers work by writing your own is a terrific way to learn.  If you intend to get into programming as a profession, especially in games, you will find that STL is not allowed (or, more correctly, has been reimplemented).  Either way, I think that understanding the underlying principles of STL will make you a better programmer along the way.   ;Quote:Original post by argonautLearning how standard containers work by writing your own is a terrific way to learn.  If you intend to get into programming as a profession, especially in games, you will find that STL is not allowed (or, more correctly, has been reimplemented).  Either way, I think that understanding the underlying principles of STL will make you a better programmer along the way.haha, thanks, that wasn't really the reason why I want to write my own list thoughI wanted to write my own with template was for two reasons:1) This is my first time using templates2) I had bad experiences stuff other people wrote, and took me a long long time looking for documentation and error codes, so I'd prefer to write my own thing and know exactly what's going on rather than relying on what other people has done.Besides, I get a lot of flexibility this way, right now, I don't know what special feature I want to add, but I'm anticipating for some, in which case, i can inherit the class and do whatever I want to it.   ;  If you're doing this to practice template use, why don't you template the nodes themselves as well as the list? E.g.:template<typename T>class Node {    T value;    Node* next;};template<typename T>class List {    Node<T>* first;    //...};That way your list will be able to work with any data type without you having to declare a node type for that type.   ;Quote:Original post by Oberon_CommandIf you're doing this to practice template use, why don't you template the nodes themselves as well as the list? E.g.:*** Source Snippet Removed ***That way your list will be able to work with any data type without you having to declare a node type for that type.oh yeah, good point, I didn't really think about that   