why doesnt this work?

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
120
121
122
123
124
125
#ifndef CPP_TESTSTACK2
#define CPP_TESTSTACK2

#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>

using namespace std;

// Specification  file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
  struct node       // Each node has two fields:
       {
        type data;       //   a data field,
        node<type> *next;     //   and a pointer field.
       };

template<class type>
class stack
  {
   private:
      node<type> *head;      // Pointer to the first cell.

   public:
        stack();
        void push(node<type>*& head, type item);
        bool pop(node<type>*& head);
        bool search(type item);
        void view();
        bool empty();
        bool full();

};
template<class type>
void stack<type> :: push (node<type>*& head, type item)
        {
                node<type> *t;
                t-> data=item;
                t-> next=head;
                head = t;

        }
//   Function to remove the first element from the stack and
//   return it to the caller.
template<class type>
bool stack<type> ::  pop (node<type>*& head)
   {

        node<type>* cur;
        cur = head;
        if(cur == NULL)
        {
                return false;
        }

        else
        {
                head = cur -> next;
                delete cur;
                return true;
 }
        }

// Accessor functions.
//   Function to see whether an element is on the list.
template<class type>
bool stack<type> :: search (type item)
        {
        node<type>* cur=head;
        while(cur != NULL && cur -> data != item)
        {
                cur=cur -> next;
        }
                  return cur;
        }
//   Function to output the list for viewing.  Assumes the
//   data type is compatible with cout << .
template<class type>
void stack<type> :: view ()
        {
                node<type>* tmp = head;
        while(tmp != NULL)
        {
                cout<< tmp -> data << endl;
                tmp = tmp -> next;
        }
        }
//   Is the list empty?
template<class type>
bool stack<type> ::  empty ()
        {
                if(head-> next = NULL)
                   return true;
                else return false;
        }

//   Is the list full?
template<class type>
bool stack<type> :: full ()
        {
                return false;
        }

#endif


int main(){

stack<int>  a;

a.push(0);
a.push(4);
a.push(3);

a.view();





}


Its a code that takes any data type into a linked list.

Output:

teststack2.cpp: In function ‘int main()’:
teststack2.cpp:114: error: no matching function for call to ‘stack<int>::push(int)’
teststack2.cpp:38: note: candidates are: void stack<type>::push(node<type>*&, type) [with type = int]
teststack2.cpp:115: error: no matching function for call to ‘stack<int>::push(int)’
teststack2.cpp:38: note: candidates are: void stack<type>::push(node<type>*&, type) [with type = int]
teststack2.cpp:116: error: no matching function for call to ‘stack<int>::push(int)’
teststack2.cpp:38: note: candidates are: void stack<type>::push(node<type>*&, type) [with type = int]

Your member function "push" defined on line 38 requires two arguments. One is of your 'node' type, the other is an integer. I see you only passing an integer on lines 114 115 and 116.
we thought that but did not know what to pass in as the node type, that parameter is supposed to pass in the head pointer automatically with out declaring it
closed account (zb0S216C)
You answer is already in front of you:
candidates are: void stack<type>::push(node<type>*&, type)


Basically, a.push(0)( along with the other push calls ) don't match the argument set you defined in your template. You defined push as:
void push(node<type>*& head, type item);


where you called it as:
a.push(int), a.push(int), a.push(int);
Last edited on
@OP: That's not what that does. As a private data member your "head" function is accessible by any member of that class, but it doesn't get passed in by default like that. Delete that part of the function declaration and try accessing "head" as if it were a global variable to that member functions of that class.
Last edited on
we have tried what you all are saying but still no luck... have any of you put it into a compiler and got it to work with the suggestions you all are saying? if u did can u post how you put it in because we may just be typing it in wrong.
i made it just pass in a (type item) parameter but then when i compile using g++ -o i get the error

/tmp/ccZVWIP7.o(.text+0x120): In function `main':
: undefined reference to `stack<int>::stack()'
collect2: ld returned 1 exit status

so not sure what its trying to say here
Topic archived. No new replies allowed.