C++ 从栈中获取指针

标签 c++

我有这种格式的类(class):

class polarity {
public:
    void method1();
    void method2();


    polarity();
    ~polarity();
private:

    stack<type*>  carbon;
    vector<scientist*>   sci;

};

这是方法一:

void polarity::method1(){

    type * object1;
    object1 = new type();
    unsigned int i;


        for(i = 0; i <carbon.size() ; ++i){         
            object1 = carbon.pop();
            /*Do something with object1*/
        }
} 

当我运行程序时我收到这个错误

Error   1   error C2440: '=' : cannot convert from 'void' to 'type *'   

我不明白这是什么意思。我只是用一些类型的指针填充堆栈。但我无法检索这些指针。
如果有任何帮助,我将不胜感激。

最佳答案

stack 弹出的方法返回一个 void 因为只从堆栈弹出元素也许你在寻找 carbon.top()但我不知道你想达到什么目的

关于C++ 从栈中获取指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40118819/

相关文章:

c++ - 为什么可以在 .h 接口(interface)和 .cpp 实现中拆分非模板类?

c++ - 在 QtableWidget 中将列中的文本隐藏为密码点

c++ - 在 Eigen C++ : mix complex matrix and real matrix

c++ - 异或字符串加密/解密

c++ - rand() 没有给我一个随机数(即使使用 srand() 时)

c++ - 从后面插入和从前面和后面取出的最有效容器是什么?

c++ - Qt 槽不在多线程 DLL 中执行

c++ - 在双向链表上插入函数

c++ - 数组类错误 : too many initializers 的多维数组

c++ - Q_DECL_EXPORT 关键字含义