fork download
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. template<typename T>
  5. class LinkedList
  6. {
  7. private:
  8. T element;
  9. T *next;
  10.  
  11. public:
  12. LinkedList();
  13. LinkedList(T element);
  14.  
  15. void add(LinkedList<T> &otherList);
  16. void print();
  17. };
  18.  
  19. template<typename T>
  20. LinkedList<T>::LinkedList()
  21. {
  22. next = NULL;
  23. }
  24.  
  25. template<typename T>
  26. LinkedList<T>::LinkedList(T element)
  27. {
  28. this->element = element;
  29. next = NULL;
  30. }
  31.  
  32. template<typename T>
  33. void LinkedList<T>::add(LinkedList<T> &otherList)
  34. {
  35. next = &otherList;
  36. }
  37.  
  38.  
  39. template<typename T>
  40. void LinkedList<T>::print()
  41. {
  42. LinkedList<T> *current = this;
  43. while (current != NULL)
  44. {
  45. std::cout << current->element;
  46. current = current->next;
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. LinkedList<std::string> myFirst("First");
  53. LinkedList<std::string> mySecond("Second");
  54. myFirst.add(mySecond);
  55. myFirst.print();
  56.  
  57. return 0;
  58. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void LinkedList<T>::add(LinkedList<T>&) [with T = std::basic_string<char>]’:
prog.cpp:54:25:   required from here
prog.cpp:35:10: error: cannot convert ‘LinkedList<std::basic_string<char> >*’ to ‘std::basic_string<char>*’ in assignment
     next = &otherList;
          ^
prog.cpp: In instantiation of ‘void LinkedList<T>::print() [with T = std::basic_string<char>]’:
prog.cpp:55:19:   required from here
prog.cpp:46:17: error: cannot convert ‘std::basic_string<char>*’ to ‘LinkedList<std::basic_string<char> >*’ in assignment
         current = current->next;
                 ^
stdout
Standard output is empty