Jump to content
  • Advertisement
Sign in to follow this  
polarboy

[solved]C++ template for generic linked list

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 encountered This 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 cpp and this is my declaration and 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]

Share this post


Link to post
Share on other sites
Advertisement
Use [ source ] instead of [ code ] if you want the box ;)

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.

Also, if you want a templated linked list, why not just use std::list?

Share this post


Link to post
Share on other sites
Ah, thanks for the tip, I thought it was [code ] for some reason, no idea why,haha

anyways, I used cout to determine that it happens at list<testNode> testlist;

I looked into std::list
but the amount of information overwhelmed me,lol
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 on

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by Oberon_Command
Where do you instantiate your Test node? I don't see a malloc() or a new statement...


Ah, good point, that was dumb of me

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by argonaut
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.


haha, thanks, that wasn't really the reason why I want to write my own list though
I wanted to write my own with template was for two reasons:
1) This is my first time using templates
2) 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oberon_Command
If 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!