![]() |
| ||
| C++ Stack/GeneralTree help When I test my stack with an int when I do pop all it seems to return is the address of the thing that I want and not the value, which makes sense since I return something by reference.. but I just cant seem to figure out what to do to get around this. What I need my .pop function to do is return a node, which is a pointer, and be able to copy that pointer to another pointer. such as: //TreeNode *child = myStack.pop(); that is what I need my code to do, but it is not working, can you all help? template <typename T> this is what I have in my general tree class right now, to show you how I am implementing it #include <iostream> |
| ||
| Re: C++ Stack/GeneralTree help You're asking to return a pointer when you're really returning the address of an object that has fallen out of scope--
I'm honestly surprised you didn't get a compiler warning. Normally that happens when you attempt to return the value of a temporary object. Try--
|
| ||
| Re: C++ Stack/GeneralTree help Thanks for your reply. I changed my code as follows: T* pop(); template<typename T> T* Stack<T>::pop(){ T *value = &elems.back(); elems.pop_back(); return value; } but when I do this in my main... Stack<int> myStack; myStack.push(2); myStack.push(3); cout << myStack.pop() << endl; cout << myStack.pop(); what is displayed is still the address not the actual value... any ideas? |
| ||
| Re: C++ Stack/GeneralTree help Quote:
You're return the address the pointer is pointing to and not the value. What you want to do is dereference your method so when pop is called the actual value is displayed and not the address--
|
| All times are GMT -4. The time now is 6:29 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC