problems with my linklist

Started by
4 comments, last by 31337noob 18 years, 7 months ago
here is the problem Type is a template so i can use anything like classes, structs,char.... here is the problem. c:\Documents and Settings\Administrator\Desktop\SoldatAdmin\linklist.h(25): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Scripts' (or there is no acceptable conversion) c:\Documents and Settings\Administrator\Desktop\SoldatAdmin\linklist.h(26): error C2664: 'Scripts::Scripts(const Scripts &)' : cannot convert parameter 1 from 'int' to 'const Scripts &' Reason: cannot convert from 'int' to 'const Scripts' No constructor could take the source type, or constructor overload resolution was ambiguous c:\Documents and Settings\Administrator\Desktop\SoldatAdmin\linklist.h(26): error C2553: no legal conversion of return value to return type 'Scripts' Type push_back(Type data) { //the errors point to here. if(data == NULL) return NULL; what do i do?
Advertisement
Well the problem seems to be that you are trying to instantiate your linked list with a type that you can't compare to NULL. You'll need to change your logic since comparing types to NULL only makes sense for pointers.
Quote:Original post by SiCrane
Well the problem seems to be that you are trying to instantiate your linked list with a type that you can't compare to NULL. You'll need to change your logic since comparing types to NULL only makes sense for pointers.


then how can i make my linklist to accept structs and classes.
i think i got it.

there are no errors now.
If you pass by value like that then there is no invalid value you can test against, since every object must be valid (as an object that is, it could have an internal fail bit set, but you can't really deal with that). So don't test, just store the object. You could also specialise your member function for pointers to objects, and keep the test in that code, since pointers can be tested for nullness.

I'd recommend having a look at the implementation of std::list which came with your compiler. The names will be pretty wierd (lots of underscores that only library writers are allowed to use) but it will show you how to write a good linked list. Besides, once you've learnt how to write a linked list you'll be throwing it away in preference for std::list anyway, right?

Enigma
my link list sortof works now.

the head of the linklist is always changing which is not suposed to do when there is new data. why would it be doing this?

Type *push_back(Type *data)
{
if(data == NULL)
return NULL;

if(head == NULL)
{
head = new Node(data);
//head->data = data;
tail = head;
cur = head;

}
else
{
tail->next = new Node(data);
//tail->next->data = data;
tail = tail->next;
cur = tail;
}

return tail->data;
}

what is wrong?

This topic is closed to new replies.

Advertisement