0

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.

9
  • What is type? Commented Oct 18, 2016 at 22:03
  • it is another object. Commented Oct 18, 2016 at 22:04
  • 2
    Take a look at the documentation for std::stack... A quick search will tell you what the top() and the pop() functions do. Commented Oct 18, 2016 at 22:04
  • IMO, type is a poor naming choice for a data structure. Commented Oct 18, 2016 at 22:05
  • would you please specify which one? Commented Oct 18, 2016 at 22:05

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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().

1 Comment

Can someone please explain why the downvote, So i can improve my answer? Thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.