Изменение шаблона вызывает ошибку "Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'" - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте. Возникла проблема. Имеется рабочий код вида
Листинг программы
  1. public class SimpleElm
  2. {
  3. public string value;
  4. public SimpleElm next;
  5. public SimpleElm(string value)
  6. {
  7. this.value = value;
  8. this.next = null;
  9. }
  10. }
  11. public static class List
  12. {
  13. ...
  14. static public bool Delete (ref SimpleElm firstElm, int n)
  15. {
  16. SimpleElm currentElm = firstElm;
  17. if (currentElm == null) return false;
  18. if (n == 1)
  19. {
  20. firstElm = firstElm.next;
  21. return true;
  22. }
  23. for (int i = 1; i < n - 1; i++)
  24. {
  25. if (currentElm.next == null) return false;
  26. currentElm = currentElm.next;
  27. }
  28. if (currentElm.next == null) return false;
  29. currentElm.next = currentElm.next.next;
  30. return true;
  31. }
  32. }
Но если попробовать применить шаблон и заменить, то начинает ругаться
Листинг программы
  1. static public bool Delete<T> (ref T firstElm, int n) where T: SimpleElm
  2. {
  3. T currentElm = firstElm;
  4. if (currentElm == null) return false;
  5. if (n == 1)
  6. {
  7. firstElm = firstElm.next;
  8. return true;
  9. }
  10. for (int i = 1; i < n - 1; i++)
  11. {
  12. if (currentElm.next == null) return false;
  13. currentElm = currentElm.next;
  14. }
  15. if (currentElm.next == null) return false;
  16. currentElm.next = currentElm.next.next;
  17. return true;
  18. }
Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'. An explicit conversion exists (are you missing a cast?)
Как это исправить?

Решение задачи: «Изменение шаблона вызывает ошибку "Cannot implicitly convert type 'ExamList.Form1.SimpleElm' to 'T'"»

textual
Листинг программы
  1. class List<T>
  2. {
  3.     class Node
  4.     {
  5.         public T value { get; set; }
  6.         public Node Next { get; set; }
  7.  
  8.         public Node(T value)
  9.         {
  10.             this.value = value;
  11.         }
  12.     }
  13.  
  14.     private Node Head { get; set; }
  15.  
  16.     public void Add(T item)
  17.     {
  18.         if (Head == null)
  19.             Head = new Node(item);
  20.         else
  21.         {
  22.             ...
  23.         }
  24.     }
  25. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы