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
SinglyLinkedList<T>*andSinglyLinkedList<T>?SinglyLinkedList<T>*, but you're returningSinglyLinkedList<T>. 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 toSinglyLinkedList<T>will probably solve your problem (assuming proper rule-of-three etiquette has been followed).SinglyLinkedList<T> list1;should beSinglyLinkedList<T> *list1 = new SinglyLinkedList<T>();, and alllist1.in the succeeding code should belist1->. 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.