Sign in to follow this  
polarboy

[solved]C++ template for generic linked list

Recommended Posts

polarboy    145
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
Hodgman    51231
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
polarboy    145
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
Oberon_Command    6078
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
polarboy    145
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
argonaut    100
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
polarboy    145
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
Oberon_Command    6078
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
polarboy    145
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
iMalc    2466
By all means, try and write your own linked list for learning purposes. Just don't let that be "instead of" learning how to use std::list etc, or you'll keep finding an excuse to not use something that really is very well done, very powerful and very easy to use if you only investigate it.
If you've got the time to make your own then there's obviously plenty of time to do both, and doing both at the same time would probably even save you time.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by argonaut
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).
I don't think that's true in the general case. Some game studios avoid using the STL-based portions of the SC++L entirely; some create their own implementations or implement custom container classes; some use the SC++L containers with custom allocators; and some use the SC++L containers as is. I could be wrong about this I suppose, but I'm pretty sure that to say that, in general, using SC++L containers in professional game development is 'not allowed', is incorrect.
Quote:
Original post by polarboy
1) This is my first time using templates
Good reason.
Quote:
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.
Bad reason (IMO).

The SC++L isn't a third-party library - it's part of the language. It's well documented online and elsewhere, so you shouldn't have any trouble finding the information that you need.

As for 'relying on what other people have done', this is a case where you absolutely should rely on what other people have done. Assuming you're using a widely-used implementation of the SC++L, the code is practically guaranteed to be reliable and to be maximally efficient given the specifications.
Quote:
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.
Don't forget YAGNI. Also, it's often better to implement 'special features' not through inheritance, but rather via wrappers around standard containers, or as non-member functions that operate on standard containers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this