reverse an array(convert from void to int error)

I need to write a program which reverse an array using a stack.I have one error which says that can not convert from void to int and I don't know how to approach the error.
the functions. (pop and inverseOrder)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void pop(Stack& S)
{
	Stack aux = S;
	S = S->link;
	delete(aux);
}

void inverseOrder(int v[], int n)
{
	Stack S;
	initStack(S);
	for (int i = 0; i < 0; ++i)
	{
		push(S, v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		v[i] =pop(S); // here is the error.How to approach this?
	}
	cout << "Reversed array is: ";
	display(v, n);
}
v[i] =pop(S);The return type of your function, pop, is void. That means it doesn't return anything. Yet you are trying to assign the would-be return value of pop(S) to v[i].

Here is an example of a function that returns something:
1
2
3
4
5
6
7
8
int something()
{
    return 43;
}

// ...

int thing = something();



What is the definition of "Stack" in your code. If it isn't a pointer, then calling delete aux; doesn't make sense.
Last edited on
Here's the code:
main file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "header.h"
using namespace std;

int main()
{

	int n;
	int v[30];
	cout << "Enter the nr of elements: ";
	cin >> n;
	readArray(v, n);
	inverseOrder(v, n);
	return 0;
}

functions 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
#include <iostream>
#include "header.h"
using namespace std;

void initStack(Stack& S)
{
	S = nullptr;
}

void push(Stack& S, int a)
{
	Element*nou = new Element;
	nou->data = a;
	nou->link = S;
	S = nou;
}

void pop(Stack& S)
{
	Stack aux = S;
	S = S->link;
	delete(aux);
}

bool isEmpty(Stack &S)
{
	if (S == 0)
		return true;
	else
		return false;
}

void readArray(int v[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		cout << "v[" << i << "]=";
		cin >> v[i];
	}
}

void display(int v[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		cout << v[i] << " ";
	}
}

void inverseOrder(int v[], int n)
{
	Stack S;
	initStack(S);
	for (int i = 0; i < n; ++i)
	{
		push(S, v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		v[i] =pop(S);
	}
	cout << "Reversed array is: ";
	display(v, n);
}

header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef HEADER_H_
#define HEADER_H_

struct Element {
	int data;
	Element* link;
};
typedef Element* Stack;
void initStack(Stack& S);
void push(Stack& S, int a);
void pop(Stack& S);
bool isEmpty(Stack &S);
void readArray(int v[], int n);
void display(int v[], int n);
void inverseOrder(int v[], int n);

#endif 

Last edited on
Okay so your "Stack" is actually an individual Element pointer that you typedef'd.
So perhaps you want to return something from your pop() function, as I mentioned before.

1
2
3
4
5
6
7
8
9
10
int pop(Stack& S)
{
	int value = S->data;
	// your existing code:
	Stack aux = S;
	S = S->link;
	delete aux;
	// return:
	return value;
}

Not tested. There might be other issues.
Yes, it's working now.Thank you for your help!
Topic archived. No new replies allowed.