help me to understand these errors

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
machine.cpp: In member function `bool bottleStack::pop(int)':
machine.cpp:24: invalid types `int[int]' for array subscript
machine.cpp: In member function `bool bottleStack::peek(int)':
machine.cpp:33: invalid types `int[int]' for array subscript
machine.cpp: At global scope:
machine.cpp:55: ISO C++ forbids declaration of `linkedstack' with no type
machine.cpp:55: no `int linkedStack::linkedstack(int)' member function declared 
   in class `linkedStack'
machine.cpp: In member function `int linkedStack::linkedstack(int)':
machine.cpp:56: invalid conversion from `int*' to `int'
machine.cpp: At global scope:
machine.cpp:60: declaration of `void linkedStack::pushlink(int)' outside of 
   class is not definition
machine.cpp:61: parse error before `{' token
machine.cpp:63: syntax error before `->' token
machine.cpp:64: syntax error before `->' token
machine.cpp:65: ISO C++ forbids declaration of `front' with no type
machine.cpp:65: `ptr' was not declared in this scope
machine.cpp:67: parse error before `}' token
machine.cpp:70: syntax error before `::' token
machine.cpp:70: `DataType' was not declared in this scope
machine.cpp:70: `poppedElement' was not declared in this scope
machine.cpp:71: ISO C++ forbids declaration of `poplink' with no type
machine.cpp:71: syntax error before `{' token
machine.cpp:74: `DataType' was not declared in this scope
machine.cpp:74: non-template type `Node' used as a template
machine.cpp:74: ISO C++ forbids declaration of `ptr' with no type
machine.cpp:74: base operand of `->' is not a pointer
machine.cpp:75: ISO C++ forbids declaration of `poppedElement' with no type
machine.cpp:75: request for member `info' in `*ptr', which is of non-aggregate 
   type `int'
machine.cpp:76: syntax error before `->' token
machine.cpp:85: parse error before `{' token
machine.cpp:88: ISO C++ forbids declaration of `topElement' with no type
machine.cpp:88: base operand of `->' is not a pointer
machine.cpp:89: parse error before `return'
machine.cpp:92: non-member function `bool isEmptylink()' cannot have `const' 
   method qualifier
machine.cpp:93: parse error before `{' token

main program file:
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
#include <iostream>
#include <string>
#include <sstream>
#include "Machines2.h"
using namespace std;



int main()
{
	bottleStack bottle;
	linkedStack Can;
	bool x,y;
    char type;

    int selection;	
    
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;
while(selection != 5)
{

 	if (selection = 1)
   	{
     cout << "How many  bottle of soda would you like to add number of "<< endl;
     cin >> x;
        bottle.push(x);
	 cout << "How many  can of soda would you like to add number of " << endl;
	 cin >> y;
	Can.pushlink(y);
  	}
	if (selection = 2)
	{
		cout << "how many drinks would you like ?" << endl;
		cin >> x;
		cout << "Would you like a bottle(B) or Can(C)?"<< endl;
		cin >> type;
		if (type = 'B')
		{ 
			bottle.pop(x);
		}
		else 
		      Can.poplink(x);
	}

	if (selection = 5)
	{
	cout << "quit" <<endl;
	}
}
return 0;
}

header file:
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
#include <iostream>
#include <string>

using namespace std;

  class bottleStack // Array Stack
{
   public:
 bottleStack();
 void push(int pushelement);  
 bool pop( int popelement );
 bool peek(int peekelement); 
 bool isEmpty( ) const;  
 void makeEmpty( );
   private:
        int  elements;
	   int top;
};

// Linked List
struct Node {
       int  info;
       int *next;
};


    class linkedStack // Array Stack
    {
   public:
 linkedStack();
 void pushlink( int  elementToPush );  
 bool poplink( int poppedElement );
 bool peeklink (int topElement ); 
 bool isEmptylink( ) const;  
 void makeEmptylink( );
   private:
     int  *front;
     int  *back;
     int  header;

};


implementation file
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
//Machine.cpp

#include <iostream>
#include "Machines2.h"


bottleStack::bottleStack( ):elements(2),top(-1)
{
}

void bottleStack::push( int pushelement )
{
	elements = pushelement;
	top++;

}

bool bottleStack::pop( int poppedElement )
{
if (top == -1)
          return false;
for (int x = 0; x < elements; x++)
	{
     poppedElement = elements[top]; // stores popped element into variable
   top--;
}
}

bool bottleStack::peek( int topElement )
{
if (top == -1)
    return false;
topElement = elements[top];
    return true;
}


void bottleStack::makeEmpty()
{
top = -1;
}


bool bottleStack::isEmpty() const
{
if (top == -1)
   return true;
return false;
}

//LinkedList Stack


linkedStack::linkedstack( int front )
{
front = back = &header;
}


 void linkedStack::pushlink( int 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 peeklink( DataType & topElement ); 
{
if (front == back)
    return false;
topElement = front->next->info;
    return true;
}
template <class DataType> 
bool isEmptylink( ) const;  // returns true if list is empty otherwise, returns false
 {
if (front == back)
   return true;
return false;
}

template <class DataType>
void makeEmptylink( )
{
DataType temp;
while(poplink(temp));
}


and this is a brief summary of what the program is supposed to do
You are to implement a program that uses both a Array-based Stack and a Linked Stack (the choice of how to use them is up to you!). The program will mimic two typical soda machines – one that holds cans and one that holds bottles. As you know, sodas are put into the machine in LIFO order. Your program will stock the machines with sodas, and then allow the user to purchase sodas and/or retrieve information about the machines, using the appropriate Stack functions.
Some of the errors:

1
2
machine.cpp: In member function `bool bottleStack::pop(int)':
machine.cpp:24: invalid types `int[int]' for array subscript

elements is an int, not an array, so you cannot subscript an int

1
2
3
4
linkedStack::linkedstack( int front )
{
front = back = &header;
}
You did not define a constructor that takes an int. Also, which front are you modifying, the one from the input, or the member of the class

On line 60
void linkedStack::pushlink( int elementToPush ); - no semicolon at the end

Try to read the error messages, in order, since some errors might be caused by something else
Last edited on
Line 20 and 21 of implementation file has no bracket for if statement

1
2
if (top == -1)
          return false;


The same error in the implementation file with lines
31,32
46,47
72,73
77,78
86.87
94,95
hey guys thanks for the help. I tried to change what you guys suggested and then I just attempted to take the program in another direction and got these errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
program3.cpp: In function `int main()':
program3.cpp:28: template-argument `main()::soda' uses local type `main()::soda
   '
program3.cpp:28: ISO C++ forbids declaration of `bottle' with no type
program3.cpp:30: template-argument `main()::soda' uses local type `main()::soda
   '
program3.cpp:30: ISO C++ forbids declaration of `Can' with no type
program3.cpp:62: request for member `push' in `bottle', which is of 
   non-aggregate type `int'
program3.cpp:74: request for member `pop' in `bottle', which is of 
   non-aggregate type `int'
program3.cpp:77: request for member `poplink' in `Can', which is of 
   non-aggregate type `int'
program3.cpp:85: parse error before `return'
program3.cpp:33: confused by earlier errors, bailing out
machine.cpp:5: syntax error before `::' token
machine.cpp:5: ISO C++ forbids declaration of `Stack' with no type
machine.cpp: In function `int Stack()':
machine.cpp:6: only constructors take base initializers
machine.cpp:6: confused by earlier errors, bailing out

header file:
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
#include "machine.cpp" 
#include <iostream>

#include<string>


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

	// removes an element from the top of the stack and returns it in poppedElement; 
	// returns false if called on an empty stack; otherwise, returns true
 bool pop( DataType & poppedElement );

	// returns the element at the top of the stack in topElement without removing it
	// returns false if called on an empty stack; otherwise, returns true
 bool peek( DataType & topElement ); 
 bool isEmpty( ) const;  // returns true if the stack is empty;
	// otherwise, returns false
 void makeEmpty( )
   private:
     Array<DataType> elements;
	   int top;
};

// Linked List
template<class DataType>
struct Node {
       DataType info;
       Node<DataType>*next;
};

template <class DataType>
    class linkedlistStack // Array Stack
    {
   public:
 Stacklink( );
 void pushlink( DataType elementToPush );  

	// removes an element from the top of the stack and returns it in poppedElement; 
	// returns false if called on an empty stack; otherwise, returns true
 bool poplink( DataType & poppedElement );

	// returns the element at the top of the stack in topElement without removing it
	// returns false if called on an empty stack; otherwise, returns true
 bool peeklink( DataType & topElement ); 
 bool isEmptylink( ) const;  // returns true if the stack is empty;
	// otherwise, returns false
 void makeEmptylink( )
   private:
     Node<DataType>*front;
     Node<DataType>*back;
     Node<DataType>header;

};

implementation file:
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
#include "Machines.h"
#include<iostream>
//Machine.cpp
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));
}

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
108
109
#include <iostream>

#include <string>

#include <sstream>

#include "Machines.h"

using namespace std;




int main()

{
	
	
int x;
    
char type;

    
int selection;	
    


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

bottleStack<soda>bottle;
	
linkedStack<soda>Can;


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;

}
Topic archived. No new replies allowed.