help understanding and debugging code(Templates and Pointers)

I'm sorry I keep bombarding you guys with this problem but its really bothering me that I can't get it. This is the newest batch of errors i get

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
In file included from program3.cpp:6:
Machines.h:19: 'Array' is used as a type, but is not defined as a type.
Machines.h:26: declaration of `class DataType'
Machines.h:24:  shadows template parm `class DataType'
Machines.h:28: too many template parameter lists in declaration of `
   linkedlistStack'
Machines.h:31: data member `info' cannot be a member template
Machines.h:32: non-template type `Node' used as a template
Machines.h:32: ISO C++ forbids declaration of `next' with no type
Machines.h:32: data member `next' cannot be a member template
Machines.h:35: ISO C++ forbids declaration of `Stacklink' with no type
Machines.h:45: non-template type `Node' used as a template
Machines.h:45: ISO C++ forbids declaration of `front' with no type
Machines.h:45: data member `front' cannot be a member template
Machines.h:46: non-template type `Node' used as a template
Machines.h:46: ISO C++ forbids declaration of `back' with no type
Machines.h:46: data member `back' cannot be a member template
Machines.h:47: non-template type `Node' used as a template
Machines.h:47: ISO C++ forbids declaration of `header' with no type
Machines.h:47: data member `header' cannot be a member template
In file included from machine.cpp:2,
                 from program3.cpp:7:
Machines.h:8: redefinition of `class bottleArrayStack<DataType>'
Machines.h:8: previous definition of `class bottleArrayStack<DataType>'
Machines.h:19: 'Array' is used as a type, but is not defined as a type.
Machines.h:26: declaration of `class DataType'
Machines.h:24:  shadows template parm `class DataType'
Machines.h:28: too many template parameter lists in declaration of `
   linkedlistStack'
Machines.h:31: data member `info' cannot be a member template
Machines.h:32: non-template type `Node' used as a template
Machines.h:32: ISO C++ forbids declaration of `next' with no type
Machines.h:32: data member `next' cannot be a member template
Machines.h:35: ISO C++ forbids declaration of `Stacklink' with no type
Machines.h:45: non-template type `Node' used as a template
Machines.h:45: ISO C++ forbids declaration of `front' with no type
Machines.h:45: data member `front' cannot be a member template
Machines.h:46: non-template type `Node' used as a template
Machines.h:46: ISO C++ forbids declaration of `back' with no type
Machines.h:46: data member `back' cannot be a member template
Machines.h:47: non-template type `Node' used as a template
Machines.h:47: ISO C++ forbids declaration of `header' with no type
Machines.h:47: data member `header' cannot be a member template
In file included from program3.cpp:7:
machine.cpp:54: ISO C++ forbids declaration of `Stacklink' with no type
machine.cpp:54: no `int linkedlistStack<DataType>::Stacklink()' member function 
   declared in class `linkedlistStack<DataType>'
machine.cpp:54: template definition of non-template `int 
   linkedlistStack<DataType>::Stacklink()'
machine.cpp:59: confused by earlier errors, bailing out
In file included from machine.cpp:2:
Machines.h:19: 'Array' is used as a type, but is not defined as a type.
Machines.h:26: declaration of `class DataType'
Machines.h:24:  shadows template parm `class DataType'
Machines.h:28: too many template parameter lists in declaration of `
   linkedlistStack'
Machines.h:31: data member `info' cannot be a member template
Machines.h:32: non-template type `Node' used as a template
Machines.h:32: ISO C++ forbids declaration of `next' with no type
Machines.h:32: data member `next' cannot be a member template
Machines.h:35: ISO C++ forbids declaration of `Stacklink' with no type
Machines.h:45: non-template type `Node' used as a template
Machines.h:45: ISO C++ forbids declaration of `front' with no type
Machines.h:45: data member `front' cannot be a member template
Machines.h:46: non-template type `Node' used as a template
Machines.h:46: ISO C++ forbids declaration of `back' with no type
Machines.h:46: data member `back' cannot be a member template
Machines.h:47: non-template type `Node' used as a template
Machines.h:47: ISO C++ forbids declaration of `header' with no type
Machines.h:47: data member `header' cannot be a member template
machine.cpp:54: ISO C++ forbids declaration of `Stacklink' with no type
machine.cpp:54: no `int linkedlistStack<DataType>::Stacklink()' member function 
   declared in class `linkedlistStack<DataType>'
machine.cpp:54: template definition of non-template `int 
   linkedlistStack<DataType>::Stacklink()'
machine.cpp:59: confused by earlier errors, bailing out 

main program:
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
#include <iostream>

#include <string>

#include <sstream>

#include "Machines.h"

using namespace std;




int main()

{
	
bottleStack bottle;
	
linkedStack Can;
	
int x;
    
char type;

    
int selection;	
    


struct soda
{
string name;
char type;
float price;
} drink;

do
{

 
cout<<"\n Menu (order 1-4, 5 exit)"<<"\n";

cout<<"\n 0thers: Exit";

cout<<"\n 1: Add another soda to the machine ";

cout<<"\n 2: Buy a soda ";

cout<<"\n 3: Find a soda by name ";

cout<<"\n 4: How much is the soda ";

cout<<"\n 5: QUIT"; 
cout<<"\n\n Enter a choice : ";

cin >> selection;

	
if (selection = 1)
   	
{
     
cout << "What is the name of the soda you would like to add?"<< endl;

     cin >> drink.name;

cout << "Is the soda a Can or a Bottle?"<< endl;
        cin >> drink.type;
	 cout << "How much is the soda? (Value between $0.0 and $1.25)" << endl;

	 cin >> drink.price;

        bottle.push(drink);


  	}
	
if (selection = 2)

	{
	
	cout << "Would you like a bottle(B) or Can(C)?"<< endl;

		cin >> type;
		
if (type = 'B' || 'b')

bottle.pop(drink);
else 
		      
Can.poplink(drink);
	}

	
if (selection = 5)
	
cout << "quit" << endl;

} while(selection != 5)



return 0;

}

header file Machines.h:
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
#include <iostream>

#include<string>


using namespace std;
template <class DataType>
    class bottleArrayStack // Array Stack
    {
   public:
 Stack( );
 void push( DataType elementToPush );  

 bool pop( DataType & poppedElement );

 bool peek( DataType & topElement ); 
 bool isEmpty( ) const; 
 void makeEmpty( );
   private:
     Array<DataType> elements;
	   int top;
};

// Linked List
template<class DataType>

template <class DataType>
    class linkedlistStack // Array Stack
    {
   public:
struct Node {
       DataType info;
       Node<DataType>*next;
};

 Stacklink( );
 void pushlink( DataType elementToPush );  

 bool poplink( DataType & poppedElement );

 bool peeklink( DataType & topElement ); 
 bool isEmptylink( ) const;  
	
 void makeEmptylink( );
   private:
     Node<DataType>*front;
     Node<DataType>*back;
     Node<DataType>header;

};
'
Implementation file machine.cpp
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
//machine.cpp
#include "Machines.h"
#include<iostream>
template <class DataType>
bottleArrayStack<DataType>::Stack( ):elements(2),top(-1)
{
}
template <class DataType>
void bottleArrayStack<DataType>::push( DataType elementToPush )
{
if(++top== elements.length()) // checks to see if array is full
   elements.changeSize(elements.length()<<1); // doubles array if it's full
elements[top] = elementToPush;
}

template <class DataType>
bool bottleArrayStack<DataType>::pop( DataType & poppedElement )
{
if (top == -1)
          return false; // returns false if array is empty
poppedElement = elements[top]; // stores popped element into variable
top--;
}

template <class DataType>
bool bottleArrayStack<DataType>::peek( DataType & topElement )
{
if (top == -1)
    return false;
topElement = elements[top];
    return true;
}

template <class DataType>
void bottleArrayStack<DataType>::makeEmpty()
{
top = -1;
}

template <class DataType>
bool bottleArrayStack<DataType>::isEmpty() const
{
if (top == -1)
   return true;
return false;
}

//LinkedList Stack

template <class DataType>
linkedlistStack<DataType>::Stacklink( )
{
front = back = &header;
}

template <class DataType>
 void linkedlistStack<DataType>::pushlink( DataType elementToPush )
{
Node<DataType>*ptr = new Node<DataType>;
ptr->info = elementToPush;
front->next=ptr;
front = ptr;

} 

template <class DataType>
bool linkedlistStack<DataType>::poplink( DataType & poppedElement )
{
if (front == back)
          return false; // returns false if list is empty
Node<DataType>*ptr=front->next;
poppedElement = ptr->info; // stores popped element into variable
front->next=ptr->next;
if (back == ptr)
    back = front;
delete ptr;

return true;
}

template <class DataType>
 bool linkedlistStack<DataType>::peeklink( DataType & topElement )
{
if (front == back)
    return false;
topElement = front->next->info;
    return true;
}
template <class DataType> 
bool linkedlistStack<DataType>::isEmptylink( ) const  // returns true if list is empty otherwise, returns false
 {
if (front == back)
   return true;
return false;
}

template <class DataType>
void linkedlistStack<DataType>::makeEmptylink( )
{
DataType temp;
while(poplink(temp));
}
closed account (Dy7SLyTq)
a) do you mean std::array? because i dont see you define an Array<class> class anywhere.
b) remove line 25 of the array file
Basically what im trying to do is imitate a soda machine using an linked list and an array based stack. I tried to make a template for each so that a user could just use an object as a data type and store drinks in the machine.

The array based stack named "bottlestackarray" would store all bottle data in an stack.

The linked list one was to be used if the type was determined to be 'c' or can.

Line 20 of the header file I put as a private member an array of "data type elements called elements.
Topic archived. No new replies allowed.