首页 > 解决方案 > 如何访问堆栈中对象的成员?C++

问题描述

如何分配/读取堆栈中对象的成员?

struct item{
    char opra; 
    int count;
    double operand;
};
stack<item> S;
double test = S.top.operand;

它不起作用,谢谢。

标签: c++

解决方案


Top 是一个方法,所以你应该调用S.top().operand. 我让它像这样编译:

#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;
}

推荐阅读