linked list and pointer compile error

I'm attempting to create a doubly linked list, and have come up with the following:

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
#ifndef LINK_LIST_H_
#define LINK_LIST_H_

#include <utility>

typedef std::pair <int, int> Item;

class Node {
  public:
    Item   data;
    Node*  next;
    Node*  prev;
    Node(Item & node_data, Node* next_node = nullptr, Node* prev_node = nullptr)
      : data(node_data), next(next_node), prev(prev_node) { }
};

class Linked {
  public:
    Node*   head;
    Node*   tail;

    // Create a head & tail as part of the constructor
    Linked(Node & node) { // Constructor
      head->next = tail;
      tail->prev = head;
      tail->next = nullptr;
      head->prev = nullptr;
    }

    ~Linked() { // destructor
      Node* tmp;
      for(;head;head = tmp) {
        tmp = head->next;
        delete head;
      }
    }

    void append(Node & last) {
      Node * tmp = this->tail->prev;
      last->prev = tmp->prev;
      last->next = tmp->next;
      tmp->next = last;
      tail->prev = last;
    }

//    void Linked::ins
};

#endif 


I get the following compile error though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
g++ -Wall -c -std=gnu++11 "linked_list.h" (in directory: /linked_list)
linked_list.h: In member function ‘void Linked::append(Node&)’:
linked_list.h:40:11: error: base operand of ‘->’ has non-pointer type ‘Node’
       last->prev = tmp->prev;
           ^
linked_list.h:41:11: error: base operand of ‘->’ has non-pointer type ‘Node’
       last->next = tmp->next;
           ^
linked_list.h:42:17: error: cannot convert ‘Node’ to ‘Node*’ in assignment
       tmp->next = last;
                 ^
linked_list.h:43:18: error: cannot convert ‘Node’ to ‘Node*’ in assignment
       tail->prev = last;
                  ^
Compilation failed.


I assumed last->prev is a pointer and so is tmp, so an assignment should be feasible.
Any clues would be appreciated?
Last edited on
Lines 40-41: last is passed by reference it is not a pointer. last->member is not valid. Use last.member.

Lines 42-43: Again, last is passed by reference. It is not a pointer. tmp->next and tail->prev are pointers. You can't assign a non-pointer type to a pointer. Use &last.


thanks for the help, I'll stick to pointers as arithmetic is possible.

so this solves the above compile errors
1
2
3
4
5
6
7
void append(Node * last) {
      Node * tmp = this->tail;
      last->prev = tmp->prev;
      last->next = tmp->next;
      tail->next = nullptr;
      tail->prev = last;
    }
thanks for the help, I'll stick to pointers as arithmetic is possible.

Pointer arithmetic for most implementations of a linked list, yours included, would be a horrible mistake. Nevertheless, I'd stick to pointers for consistency internally. Client code, of course, should have no need to know about Nodes, pointers or not.
Last edited on

Hi

thanks for the tips, I've made my linked list somewhat more generic by making the data within Node a type. When I attempt to append to the end of the list I get the following compile error:

1
2
3
4
5
6
7
8
9
10
g++ -c -Wall -std=gnu++11 track_score.cpp
track_score.cpp: In function ‘int main()’:
track_score.cpp:12:15: error: request for member ‘append’ in ‘score_sheet’, which is of non-class type ‘LinkedList<int>(Node<int>)’
   score_sheet.append(Node<Item>);
               ^
track_score.cpp:12:32: error: expected primary-expression before ‘)’ token
   score_sheet.append(Node<Item>);
                                ^
makefile:12: recipe for target 'track_score.o' failed
make: *** [track_score.o] Error 1


Modified code:

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
#ifndef LINK_LIST_H_
#define LINK_LIST_H_

template <class T>
class Node {
  public:
    T data;
    Node*  next;
    Node*  prev;
    Node(T & node_data, Node* next_node = nullptr, Node* prev_node = nullptr)
      : data(node_data), next(next_node), prev(prev_node) { }
    ~Node() {}
};

template <class T>
class LinkedList {
  private:
    Node<T>* head;
    Node<T>* tail;

  public:
    LinkedList(Node<T> * node) : head(node) {
        head->prev = tail;
        head->next = nullptr;
        tail->next = head;
        tail->prev = nullptr;
    } // Create a head & tail as part of the constructor

    ~LinkedList() { }
    void append(Node<T> * last) {
      Node<T> * tmp = this->tail;
      last->prev = tmp->prev;
      last->next = tmp->next;
      this->tail->next = nullptr;
      this->tail->prev = last;
    }
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <utility>
#include "linked_list.h"

int main() {
  typedef int Item;
  Item * i;
  *i = 6;
  LinkedList<Item> score_sheet(Node<Item>);

  *i = 7;
  score_sheet.append(Node<Item>);
  return 0;
}


Any suggestions would be appreciated. Thanks.
Problems I see:

linked_list.h
Line 23,25,26: tail is an uninitialized pointer (garbage).

main.cpp
Line 7: i is an uninitialized pointer.

Line 8. You're trying to dereference an uninitialized pointer.

Line 9,12: You're trying to pass a specialized class name. LinkedList's constructor is expecting a pointer.




Topic archived. No new replies allowed.