0

I'm new to c++, I'm asked for class work to do a function that takes a doubly linked list and copy it's elements into a singly linked list then returns it, however, I was given this function's declaration only, when I made the function it's either not returning anything or it gives me the error below, I understand the linked list code but I don't understand using a pointer and the returning part, can someone explain this to me? and how to make it work? I'm unable to find explanation to this online, or maybe I am searching the wrong keywords. Any help is appreciated.

template <class T>
SinglyLinkedList<T>* DoublyLinkedList<T>:: Function() {

SinglyLinkedList<T> list1;

DLLnode<T>*p = head;

while (p!=NULL) {

list1.addtoHead(p->value);
p=p->next;

}

return list1;

}

//error: cannot convert ‘SLL<int>’ to ‘SLL<int>*’ in return
user5789
  • 3
  • 3
  • 1
    Do you see the difference between `SinglyLinkedList*` and `SinglyLinkedList` ? – Yunnosch Apr 06 '20 at 12:00
  • Your function claims it will return `SinglyLinkedList*`, but you're returning `SinglyLinkedList`. They're not the same, A pointer to an object is not synonymous to the concrete object itself. The compiler is telling you *exactly* what the problem with the posted code is. Changing the return type to `SinglyLinkedList` will *probably* solve your problem (assuming proper rule-of-three etiquette has been followed). – WhozCraig Apr 06 '20 at 12:00
  • Yes, I tried making a pointer list1 instead I asked a question here before regarding this issue, https://stackoverflow.com/questions/61029659/how-to-return-a-linked-list-from-a-function-why-when-i-return-it-it-gives-me-a when I made it as a pointer it returned an address, answers suggested some ideas but I want to know how I can make this function work using a pointer in the function's declaration. – user5789 Apr 06 '20 at 12:05
  • @WhozCraig Yea I was told to follow the rule of three but I am still unable to do it correctly for linked lists, and I was given this function for class work, I should implement it pointer way but I don't see how – user5789 Apr 06 '20 at 12:19
  • If that's the case, `SinglyLinkedList list1;` should be `SinglyLinkedList *list1 = new SinglyLinkedList();`, and all `list1.` in the succeeding code should be `list1->`. It's syntactically correct, and dreadfully wrong in concept. Wouldn't be the first time academia told it's student body to code-stupid, so I'm sorry for that. – WhozCraig Apr 06 '20 at 12:22
  • @WhozCraig I tried this exactly in my previous question, but it returned an address, can you check it out for me? https://stackoverflow.com/questions/61029659/how-to-return-a-linked-list-from-a-function-why-when-i-return-it-it-gives-me-a – user5789 Apr 06 '20 at 12:25
  • Returning by pointer-to-object is just that; a pointer to the object. All that linked code does thereafter is print the pointer. I think that code just needed `list1 = lists.Function(); list1->print();` to work. As I said, conceptually this is a round-about way of allowing you to side-step proper implementation of RO3, and imho, it's a bad technique, but its your profs call. – WhozCraig Apr 06 '20 at 12:29

1 Answers1

1

1) Using a pointer for this is stupid. But that's what you've been told to do.

2) If you use a pointer then this function will return an address. That's what pointers are. You cannot change that. The trick is to dereference the pointer when you try to print. That way it won't print an address but will instead print what the pointer is pointing at. If you need help with this then post your printing code.

3) Here's how you do it with pointers

template <class T>
SinglyLinkedList<T>* DoublyLinkedList<T>:: Function() {
    SinglyLinkedList<T>* list1 = new SinglyLinkedList<T>();
    DLLnode<T>*p = head;
    while (p!=NULL) {
        list1->addtoHead(p->value);
        p=p->next;
    }
    return list1;
}

This is the second occaision in recent days when posters have been told to do something stupid by their university professors. Oh well.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    *"second occasion in recent days"* - lolz. I can assure you it happens far, far more often than that. Just imagine all the asinine times we *don't* see on this site because the lemmings blindly walked off the cliff. Sad, to be sure. – WhozCraig Apr 06 '20 at 12:32