Reputation: 11
How to assign/read a member of an object in a stack?
struct item{
char opra;
int count;
double operand;
};
stack<item> S;
double test = S.top.operand;
it not works, thanks.
Upvotes: 1
Views: 156
Reputation: 29
You are just making a typo. top in stack is a function, I let you here a working example:
#include <iostream>
#include <stack>
using namespace std;
struct item{
char opra;
int count;
double operand;
};
int main()
{
item a;
a.opra = 'a';
a.count = 3;
a.operand = 5.0;
stack<item> S;
S.push(a);
// Stack top
cout << S.top().operand;
return 0;
}
Output:
5
Upvotes: 1
Reputation: 1599
Top is a method, so you should call S.top().operand
. I got it to compile like that:
#include <iostream>
#include <stack>
struct item{
char opra;
int count;
double operand;
};
int main(){
std::stack<item> S;
double test = S.top().operand;
return 0;
}
Upvotes: 1