Conversion for functional-style cast from node* to iterator

Wondering if there's a way to convert node* to a iterator for a linked list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <initializer_list>
#include <iostream>

template <typename T>
class List {
  struct Node;  
  class Iterator;
 public:
  List() {
    std::initializer_list<T> empty;
  }

  List(std::initializer_list<T> ele) {
    this->operator=(ele);  
  }

std::size_t Size() const {
    int count = 0;
    Node* current = start;
    while (current != NULL) {
      count++;
      current = current->next;
    }
    return count;
  }

  void Insert(T ele) {
    Node* node = new Node{ele};
    if (this->size == 0) {
      this->start = this->end = node;
    } else {
      this->end->next = node;
      node->prev = this->end;
      this->end = node;
    }
    ++this->size;
  }

  void RemoveFront() {
    Node* front = this->start;
    this->start = front->next;
    delete front; 
  }

  T& Front() const {
     if (Size() <= 0) {
       return Front();
     }
    Node* Front = start;
    return Front->data;
  }

  List& operator=(std::initializer_list<T> ele) {
    this->size = 0;
    for (auto&& val : ele) this->Insert(val);
    return *this;
  }

  friend std::ostream& operator<<(std::ostream& out, const List& list) {
    for (auto val = list.start; val; val = val->next) {
      out << val->data;
      if (val->next) out << ",";
    }
    return out;
  }

  Iterator begin() {
    return Iterator(start);
  }

  Iterator last() {
    return Iterator(end);
  }

private:
  struct Node {
    T data;
    Node* next = nullptr;
    Node* prev = nullptr;
  };

  class Iterator {
    friend class List;
    typename List<T>::Node* current;
    List<T>* theList;
    public:
  };

  Node* start = nullptr;
  Node* end = nullptr;
  std::size_t size = 0;
};

int main() {
List<char> test = {'H', 'e', 'l', 'l', 'o'};
std::cout << test << std::endl;
test.begin();
std::cout << " = " << test.Front() << std::endl;
}


testings.cpp:68:12: error: no matching conversion for functional-style cast from
'List<char>::Node *' to 'List<char>::Iterator'
return Iterator(start);
Last edited on
Why can't you just add a constructor on line 83
83
84
explicit Iterator(List::Node *ptr)
  : current(ptr) {}

Last edited on
Topic archived. No new replies allowed.