im getting a compiling error: cannot convert ‘const LinkedList<int>’ to ‘LinkedList<int>::Node*’ in initialization
is there anyways to fix this.
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
|
#include <cstddef>
#include <initializer_list>
#include <iostream>
template <typename T>
class LinkedList {
struct Node;
public:
LinkedList() {
std::initializer_list<T> empty;
}
LinkedList(std::initializer_list<T> list) {
this->operator=(list);
}
LinkedList(const LinkedList<T>& another) {
this->operator=(another);
}
std::size_t Size() const {
int count = 0;
Node* current = start;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
void InsertBack(T element) {
Node* temp = new Node{element};
if (this->size == 0) {
this->start = this->end = temp;
} else {
this->end->next = temp;
temp->prev = this->end;
this->end = temp;
}
++this->size;
}
void Clear() {
while (this->start) {
Node* oldstart = this->start;
this->start = oldstart->next;
delete oldstart;
}
}
LinkedList& operator=(std::initializer_list<T> list) {
Node* current = list;
Clear();
this->size = 0;
for (int i = 0; i < Size(); i++) {
this->PushBack(current->data);
current = current->next;
}
return *this;
}
LinkedList& operator=(const LinkedList& another) {
Node* current = start;
Clear();
this->size = 0;
for (int i = 0; i < Size(); i++) {
this->PushBack(current->data);
current = current->next;
}
return *this;
}
friend std::ostream& operator<<(std::ostream& out, const LinkedList& ele) {
out << '[';
for (auto current = ele.start; current; current = current->next) {
out << current->data;
if (current->next) out << ", ";
}
out << ']';
return out;
}
private:
struct Node {
T data;
Node* next = nullptr;
Node* prev = nullptr;
};
Node* start = nullptr;
Node* end = nullptr;
std::size_t size = 0;
};
int main() {
LinkedList<char> test = {'W', 'o', 'r', 'l', 'd'};
std::cout <<"Hello, " <<test << std::endl;
}
| |
testings.cpp:48:11: error: no viable conversion from 'std::initializer_list<char>' to
'LinkedList<char>::Node *'
Node* current = list;
^ ~~~~
testings.cpp:13:11: note: in instantiation of member function 'LinkedList<char>::operator='
requested here
this->operator=(list);
^
testings.cpp:90:25: note: in instantiation of member function 'LinkedList<char>::LinkedList'
requested here
LinkedList<char> test = {'W', 'o', 'r', 'l', 'd'};
^
testings.cpp:52:14: error: no member named 'PushBack' in 'LinkedList<char>'
this->PushBack(current->data);
~~~~ ^
2 errors generated.