stacks & queues

i get errors like "no matching function for call to" so i know my error is from my int main, any help please ???
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
  #ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

using namespace std;

class container {
public:
	container();
	// Postcondition: all data array elements are initialized to 0; count is set to -1
	
	void insert(int value);
	//  Precondition: "data" array must not be full
	// Postcondition: if the container is not full, insert the value data array (first available location from left) and increment count by 1.  
	// 			If the data array is full, display "Container is full; program is terminated!" 
	//			and then terminates the program with exit(1) command.
	
	void remove(int & value);
	//  Precondition: container must not be empty, i.e., "count" must be greater than or equal to 0.
	// Postcondition: if data array is not empty, delete the most recently stored (rightmost) value in the container  by decrementing 
	//			          count by 1 (logical deletion)  
	//          		      if the container is empty display a message "Container is empty; program is terminated!"
	//			          and then terminate the program with exit(1) system call.
	
	void undelete(int & value);
	// Postcondition: the most recently removed value is restored or undeleted

	bool isEmpty();
	// Postcondition: return true is container is empty; return false otherwise

	bool isFull();
	// Postcondition: return true is container is full; return false otherwise

	void sort();
	//  Precondition: "data" array must not be empty and there must be at least two values stored in it.
	// Postcondition: sort all values stored in the container  in ascending order
	
void display();
	// Postcondition: displays all values currently stored in the contianer

private:
	const static int CAPACITY = 10;		// specifies storage capacity of a container object, the container
	int data[CAPACITY];			// stores inserted integer values  
	int count;					// (count + 1) indicates how many values have currently stored in data array
};
#endif


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
#include <iostream>
#include <iomanip>
#include <cstdlib>	// for exit() function.
#include <ctime>
using namespace std;

#include "container.h"

container::container()
{
	count = -1;	// container is empty
	for (int i = 0; i < CAPACITY; i++)
		data[i] = 0;
}
void container::undelete(int &value)
{ 
	if (!isFull())
	{
		data[--count] = value;		
		cout << setw(4) << value << " has been restored in data[" << count << "]." << endl;
	}
	else {
		cout << "Attempts to restore a value into a full container; program is terminated!";
		exit (1);
	}
}

bool container::isEmpty()
{ 
	return( count == -1 );
}

bool container::isFull()
{ 
	return( count == CAPACITY -1 );
}

void container::insert(int value)
{
	if (!isFull())
	{
		data[++count] = value;		
		cout << setw(4) << value << " has been inserted in data[" << count << "]." << endl;
	}
	else {
		cout << "Attempts to insert a value into a full container; program is terminated!";
		exit (1);
	}
}

void container::remove(int &value)	// logical removal
{
	if( isEmpty( ) ) // the stack is empty
{
cout << endl << "Stack is empty" << endl;
exit( 1 );
}
value = data[ --count ];
}

void container::sort()
{
	int pass, c, temp;
	if (count >= 1)		// sort only when the data array contains at least two elements
	{
		for (pass = 1; pass < count; pass++)
			for (c = 0; c <= count - pass; c++)
				if (data[c] > data[c + 1]) {
					temp = data[c];
					data[c] = data[c + 1];
					data[c + 1] = temp;
				}
	}
}

void container::display()
{
	
	cout << "The 'container' contains the following " << count + 1 << " value(s):\n";
	if (count == -1)
		cout << "*** data array is empty!" << endl;
	else
	{
		for (int i = 0; i <= count; i++)
			cout << data[i] << '\t';
		cout << endl;
	}
}


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 <iomanip>
#include <cstdlib>	// for exit() function.
#include <ctime>
using namespace std;

#include "container.h"

int main()
{
	
	container c;
	srand(static_cast<unsigned int>(time(0)));

	cout << "We now insert 10 random integer values into an empty container:\n";
	for (int i = 0; i < 10; i++)
		c.insert(rand() % 99 + 1);

	cout << "\nThe contents of the container are: " << endl;
	c.display();

	c.sort();
	cout << "After sort: " << endl;
	c.display();

	cout << "We now remove all values in the container:\n";

	c.remove(rand() % 99 + 1);

	cout << endl;
	c.display();

	cout << "\nWe now perform the 'undelete' operation three times:\n";

		c.undelete(rand() % 99 + 1);
		c.undelete(rand() % 99 + 1);
		c.undelete(rand() % 99 + 1);
	c.display();
	
	return 0;
}

Last edited on
Errors usually contain more information, so it would be better if next time you paste the whole error message.

This is the error I got from your program -
Error	3	error C2664: 'void container::undelete(int &)' : cannot convert argument 1 from 'int' to 'int &'


Same for the remove function. I fixed it by either not calling the function by reference. Or just place the random number in a variable.

1
2
int x = rand() % 99 + 1;
c.remove(x);


Edit:
A recommendation is to not use rand() because it's harmful and bad. You should instead learn about the random header file. <random> - http://www.cplusplus.com/reference/random/

If you want to know why rand is harmful, watch this presentation video - https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Last edited on
Your undelete function is essentially identical to your insert function.

main.cpp lines 35-37 insert three new random values into the container. Those lines do not restore three previous values at stated in the post condition.
Topic archived. No new replies allowed.