1

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.

3
  • The error :" 'operand' must have class/struct/union" is shown Commented Apr 2, 2020 at 12:41
  • If you want to add additional information, please edit your question. Commented Apr 2, 2020 at 12:42
  • Typo: S.top.operand --> S.top().operand? Commented Apr 2, 2020 at 12:42

2 Answers 2

1

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; } 
Sign up to request clarification or add additional context in comments.

Comments

1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.