Rommel
Rommel

Reputation: 1

C++ retrieving pointer from stack

I have class with this format:

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


    polarity();
    ~polarity();
private:

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

};

here is method1:

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*/
        }
} 

when I run the program I receive this error

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

I do not understand what it means. I just fill stack with some pointer of type. but I cannot retrieve these pointer.
I would appreciate for any help.

Upvotes: 0

Views: 79

Answers (2)

YosefMac
YosefMac

Reputation: 180

The method pop from stack return a void because only pop out the element from the stack maybe you looking for carbon.top() but I don't know what you are trying to achieve

Upvotes: 4

amanuel2
amanuel2

Reputation: 4646

pop() function is a void function:

void pop();

Removes the top element from the stack. Effectively calls c.pop_back().

Therefore when you assign a type* to a function returning void, you get that error.

As you can see here:

  object1 = carbon.pop();

when you pop carbon you return a void , which is not of type type. Hence getting the error :

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

Look here for refrence

Solution

You are looking for top() . so now you can do :

  object1 = carbon.top();

Look here for refrence about top().

Upvotes: 2

Related Questions