-1

I have problem with a stack of pointers. I have stack of pointers named ob1

stack<object*> ob1;

then I create some pointer to object and pushed into stack. when I want retrieve these pointer from stack I use this method;

object * tag;
tag = new object();
tag = ob1.pop();

but I get "error C2440: cannot convert void to object*" I am confuse what is wrong. I would appreciate for any help.

Rommel
  • 1
  • 4
  • 2
    I can't understand what you're trying to do with this code. What is `tag = new object()` for? You leak it immediately. Furthermore, `ob1.pop()` does not return a value (hence the error). Which book are you using to learn C++? Which reference are you using to look up standard library tools and functions? – Lightness Races in Orbit Oct 15 '16 at 17:56
  • I just looking a method for poping pointer from stack. but I don't know HOW? I am beginner. – Rommel Oct 15 '16 at 17:58
  • If all you want to do is pop an element from the stack, just simply write `ob1.pop();` — nothing more than that. If you want to do something you haven't told me, you'll need more code ... and you'll need to explain better what you want to do. Again, which book and reference are you using? It's hard to believe that these things are not explained therein. – Lightness Races in Orbit Oct 15 '16 at 18:01
  • Possible duplicate of [C++ stl pop doesn't return](http://stackoverflow.com/questions/23552830/c-stl-pop-doesnt-return) – Mike Oct 15 '16 at 18:01
  • I am going to use those pointer, for example showing some field of object to screen. I use tag as interface. for example after poping, I am going to use cout<< tag->getData()< – Rommel Oct 15 '16 at 18:07
  • Use the stack's `top()` method to access the topmost element before popping it. – Nico Schertler Oct 15 '16 at 18:09
  • @Rommel ignore that friend--they are either playing with you or they don't know what they're doing--[and get a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Oct 15 '16 at 18:15

1 Answers1

0

You get this error, because pop doesn't return anything.

See here, the return type is void, nothing. You'll need the top () member to get the element. N.B. pop () will call the destructor of your element.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45