I get this error when I try compiling my code is there a way I can fix this.
testings.cpp:69:48: error: indirection requires pointer operand
('List<char>::It' invalid)
for (; begin != end; ++begin) this->Insert(*begin);
^~~~~~
testings.cpp:114:12: note: in instantiation of function template specialization
'List<char>::List<List<char>::It>' requested here
List<char> first (test.begin(), test.end());
^
testings.cpp:94:13: warning: expression result unused [-Wunused-value]
this->cur;
~~~~ ^~~
testings.cpp:69:26: note: in instantiation of member function
'List<char>::It::operator++' requested here
for (; begin != end; ++begin) this->Insert(*begin);
^
testings.cpp:114:12: note: in instantiation of function template specialization
'List<char>::List<List<char>::It>' requested here
List<char> first (test.begin(), test.end());
^
1 warning and 1 error generated.
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <initializer_list>
#include <iostream>
template <typename T>
class List {
struct Node;
class It;
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->fin = node;
} else {
this->fin->next = node;
node->prev = this->fin;
this->fin = 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;
}
template <class It>
List(It begin, It end) {
for (; begin != end; ++begin) this->Insert(*begin);
}
It begin() {
return It(start);
}
It end() {
return It(fin);
}
private:
struct Node {
T data;
Node* next = nullptr;
Node* prev = nullptr;
};
class It {
friend class List;
explicit It (List::Node *ptr) : cur(ptr) {}
typename List<T>::Node* cur;
List<T>* theList;
public:
It& operator++() {
this->cur;
return *this;
}
It operator++(int) {
It temp(*this);
this->operator++();
return temp;
}
bool operator!=(It val) const {
return !(this->operator==(val));
}
bool operator==(It val) const {
return !(this->operator!=(val));
}
};
Node* start = nullptr;
Node* fin = nullptr;
std::size_t size = 0;
};
int main() {
List<char> test = {'H', 'e', 'l', 'l', 'o'};
std::cout << test << std::endl;
List<char> first (test.begin(), test.end());
}
| |