Value from an object on stack

Hello,

I put an object of class field, that contains int x and int y. I put it in a stack. The program below is working, but I would like to know how to get the values inside the satck. Something like S.top(p1.GetX). I need to read it from the stack and not accessing the method on class Field. Thanks in advance
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>

using namespace std;


class  Field
{
private:
	int x,y;
	
public:
	Field(int ,int );
	int GetX();
	
	};

Field::Field(int a,int b)
{
	x=a;
	y=b;
}
int Field::GetX()
{
	return x;
}


int main()
{
Field p1(2,5);
stack<Field> S;
S.push(p1);
S.top();
}
S.top().GetX();
Thanks, perfect!!
hey, in which file and project this program run?
Topic archived. No new replies allowed.