pointer won't hold a value

Hey guys I working on making a Deque, but right now I'm just trying to make one node with an integer value. The error I'm getting is:

(pa1 is the main class)
pa1.cpp: In function âint main()â:
pa1.cpp:12:17: error: no matching function for call to âDeque<int>::insertHead(int)â
pa1.cpp:12:17: note: candidate is:
Deque.h:39:12: note: Error_code Deque<T>::insertHead(T&) [with T = int]
Deque.h:39:12: note: no known conversion for argument 1 from âintâ to âint&â

I'm not sure what I'm doing wrong i have been stuck on this problem for hours and everything I try dosen't seem to fix it. Any help would be greatly appreciated.
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
template <typename T>
struct Node {
        T       entry;
        Node<T> *prev;
        Node<T> *next;

        Node();
        Node(T, Node<T> *p = NULL, Node<T> *n = NULL);
};




template <typename T>
class Deque{
private:
        int count;
        Node<T>* head;
        Node<T>* tail;

public:
        Deque();
        Deque(const Deque<T> &); //copy constructor
        Deque<T>& operator=(const Deque<T> &);
        //Or Deque<T> operator=(const Deque<T>);
   //return type should be the same as the parameter type

       ~Deque();

        int size() const;
        bool isEmpty() const;
        void clear();
        void traverse(void (*visit)(T &));

        Error_code removeHead(T &x);
        Error_code removeTail(T &x);
        Error_code insertHead(T &x);
        Error_code insertTail(T &x);
};

template <typename T>
Deque<T>::Deque()

{
   count = 0;
   head = NULL;
   tail = NULL;
}


template <typename T>
Error_code Deque<T>::insertHead(T &x)
{
  Node<T> *new_node, *previous, *following;
  if(count==0)
   {
      previous = NULL;
      following = head;
      new_node = new Node<T>(x,previous,following);

      head= new_node;
      count++;
      return success;
   }
}



#include<iostream>
#include "utility.h"
#include "Node.h"
#include "Deque.h"

using namespace std;

int main(){

Deque<int> dei;

dei.insertHead(2);


};

A temporary object can't bind to a non-const reference.

Change it to Error_code insertHead(const T &x); and it will work better.
Deque.h:39:12: note: no known conversion for argument 1 from int to int&


At line 80, you are passing an int to the function, but it requires a reference to an int. This would work:
1
2
int x = 2;
dei.insertHead( x );
Thanks guys that fixed the problem, but now I get the error:

/tmp/cc52HcBw.o: In function `main':
pa1.cpp:(.text+0x37): undefined reference to `Deque<int>::~Deque()'
pa1.cpp:(.text+0x52): undefined reference to `Deque<int>::~Deque()'
/tmp/cc52HcBw.o: In function `Deque<int>::insertHead(int const&)':
pa1.cpp:(.text._ZN5DequeIiE10insertHeadERKi[Deque<int>::insertHead(int const&)]+0x4e): undefined reference to `Node<int>::Node(int, Node<int>*, Node<int>*)'
collect2: ld returned 1 exit status

Any ideas on what is going wrong?
Where is the implementation for the Deque's destructor?
Thanks keskiverto completely forgot to do the implementation! Heres the new Deque, but I still get the error:
/tmp/cctYIBdh.o: In function `Deque<int>::insertHead(int const&)':
pa1.cpp:(.text._ZN5DequeIiE10insertHeadERKi[Deque<int>::insertHead(int const&)]+0x4e): undefined reference to `Node<int>::Node(int, Node<int>*, Node<int>*)'
collect2: ld returned 1 exit status


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
template <typename T>
class Deque{
private:
        int count;
        Node<T>* head;
        Node<T>* tail;

public:
        Deque();
        Deque(const Deque<T> &); //copy constructor
        Deque<T>& operator=(const Deque<T> &);
        //Or Deque<T> operator=(const Deque<T>);
   //return type should be the same as the parameter type

       ~Deque();

        int size() const;
        bool isEmpty() const;
        void clear();
        void traverse(void (*visit)(T &));

        Error_code removeHead(const T &x);
        Error_code removeTail(const T &x);
        Error_code insertHead(const T &x);
        Error_code insertTail(const T &x);
};

template <typename T>
Deque<T>::Deque()

{
   count = 0;
   head = NULL;
   tail = NULL;
}




template <typename T>
void Deque<T>::clear()
/*
Post: The List is cleared.
*/
{
   Node<T> *p, *q;

   for (p = head; p; p = q) {
      q = p->next;
      delete p;
   }
   count = 0;
   head = NULL;
}


template <typename T>
Error_code Deque<T>::insertHead(const T &x)
{
  Node<T> *new_node, *previous, *following;
  if(count==0)
   {
      previous = NULL;
      following = NULL;
      new_node = new Node<T>(x,previous,following);

      head = new_node;
      tail = head;
      count++;
      return success;
   }
}

template <typename T>
Deque<T>::~Deque()
/*
Post: The List is empty: all entries have been removed.
*/
{
   clear();
}

Undefined reference means your are referencing something that has been declared, but not defined. Just like you didn't actually write the code for the Deque destructor, you haven't actually written the code for the Node constructor.
Topic archived. No new replies allowed.