Cannont convert

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.
Last edited on
I don't get the error you posted but I got plenty of others.

You have not declared the function Size and PushBack.

Semicolon is missing at the end of line 32.

There is no variable called head_.
it has to something to do with line 9
I just can't figure it out
Last edited on
It's even more difficult for us when we don't have access to the real code, or at least the full error message. I don't see how line 9 could cause the error that you said.
Last edited on
Please post a minimal, compilable codeset that exhibits the problem.

Also, tell us the complete error message.
ok so I resubmitted the code hope that is more helpful
The thread has been marked as solved so perhaps you no longer need help but here is some feedback anyway...

You are trying to assign an std::initializer_list<T> to a Node<T>* which to me doesn't make much sense. It should be no surprise why this doesn't work.

What you probably want to do is to loop through the elements in the initializer list and add each element to the list (e.g. by calling InsertBack) one by one.
Topic archived. No new replies allowed.