push/pop printing issue

Hi, how do I print the value being popped or being pushed just to follow what's going on? I get windows error(below). I can print nStack.top() fine. Just not the others.

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
#include<iostream>
#include<stack>

using namespace std;


void fooPush(stack<int>&nStack, int val)
{
	nStack.push(val);
}

fooPop(stack<int>&nStack)
{
	nStack.pop();
	cout<<nStack.top()<<endl;

}

int main()
{

	stack<int>nStack;

	fooPush(nStack, 5);
	fooPush(nStack,10);
	fooPush(nStack,15);
	fooPop(nStack);

	nStack.top() -=2;
	cout<<nStack.top()<<endl;
	//std::cout<< "nStack.top() is now " << nStack.top() << '\n';

}


error:..\src\main.cpp:14:6: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')
cout<<nStack.pop();
^
Your error doesn't appear to match the code you posted.

Line 15 contains a cout of top(), not pop() as stated in the error message.

The error says you're trying to cout the result of pop().
You can't print a void.
http://www.cplusplus.com/reference/stack/stack/pop/

Last edited on
You are correct about pop. Between top, pop, and push, top is the only printable I think.

Below I am passing numbers through a stack Ive made with functions. The output is unexpected. Was wondering if someone could point out what is wrong:

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
#include<iostream>
#include<stack>

void fooPush(std::stack<int>&nStack, int &val)
{
	nStack.push(val);
}

void fooPop(std::stack<int>&nStack)
{
	nStack.pop();
	//cout<<nStack.top();
}

void readTop(std::stack<int>&nStack)
{
	std::cout<<nStack.top();
}

int main()
{
	std::stack<int>nStack;
	fooPush(nStack, 5);
	fooPush(nStack, 10);
	fooPush(nStack, 15);
	fooPush(nStack, 20);
	std::cout<<"Read Top"<<nStack.top()<<std::endl;
	fooPop(nStack);
	std::cout<<"Read top"<<nStack.top()<<std::endl;
	fooPop(nStack);
	std::cout<<"Read top"<<nStack.top()<<std::endl;
	fooPop(nStack);

}



Read Top20
Read top20
Read top20


Its like its reading it but not popping.
Last edited on
The code you provided doesn't compile so I don't know how you're getting any output.

In function 'int main()': 23:19: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' 4:6: note: in passing argument 2 of 'void fooPush(std::stack<int>&, int&)' 24:20: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' 4:6: note: in passing argument 2 of 'void fooPush(std::stack<int>&, int&)' 25:20: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' 4:6: note: in passing argument 2 of 'void fooPush(std::stack<int>&, int&)' 26:20: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' 4:6: note: in passing argument 2 of 'void fooPush(std::stack<int>&, int&)'

Once you fix the compile error it should work as you expect.
OK, no compile errors. Once I fixed the magical numbers with symbolics(val), it compiled and ran correctly.

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
#include<iostream>
#include<stack>

void fooPush(std::stack<int>&nStack, int &val)
{
	nStack.push(val);
	std::cout<<val<<std::endl;
}

void fooPop(std::stack<int>&nStack)
{
	std::cout<<std::endl<<nStack.top();
	nStack.pop();

}

void readTop(std::stack<int>&nStack)
{
	std::cout<<nStack.top();
}

int main()
{
	int val = 5;
	std::stack<int>nStack;
	fooPush(nStack, val);
	fooPush(nStack, val *= 2);
	fooPush(nStack, val *= 3);
	fooPush(nStack, val *= 4);
	fooPop(nStack);
	fooPop(nStack);
	fooPop(nStack);
	fooPop(nStack);
}
 


Last edited on
You could have also fixed the problems by passing by value instead of by reference.
void fooPush(std::stack<int>&nStack, int val) // Note, passing val by value instead of by reference.
Very interesting:
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
#include<iostream>
#include<stack>

void fooPush(std::stack<int>&nStack, int val)
{
	nStack.push(val);
	std::cout<<val<<std::endl;
}

void fooPop(std::stack<int>&nStack)
{
	std::cout<<std::endl<<nStack.top();
	nStack.pop();

}

void readTop(std::stack<int>&nStack)
{
	std::cout<<nStack.top();
}

int main()
{

	std::stack<int>nStack;
	fooPush(nStack, 1);
	fooPush(nStack, 2);
	fooPush(nStack, 3);
	fooPush(nStack, 4);
	fooPop(nStack);
	fooPop(nStack);
	fooPop(nStack);
	fooPop(nStack);
}



1
2
3
4

4
3
2
1


Why did pass by value work for this code as opposed by pass by reference. I know it should be obvious, but its not for me. I have passed by reference so much, I have forgotten the scenarios in which pass by value is more acceptable. Thx
Last edited on
Look at your error message:
In function 'int main()': 23:19: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'


A couple of things stand out when I read this error, first is that word const. Whenever I see this word in an error message I start looking for const correctness problems. The next thing that stands out is that "rvalue" instead of "lvalue". I believe it comes down to you can't create a reference to a const rvalue.

Topic archived. No new replies allowed.