943,621 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 940
  • C++ RSS
Jun 16th, 2008
0

C++ Stack/GeneralTree help

Expand Post »
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?



c++ Syntax (Toggle Plain Text)
  1. template <typename T>
  2. class Stack{
  3.  
  4. private:
  5.  
  6. std::vector<T> elems; // elements
  7.  
  8. public:
  9.  
  10. void push(T const&); // push element
  11. T* pop(); // pop element
  12. T top() const; // return top element
  13.  
  14. bool empty() const // return whether the stack is empty
  15. {
  16. return elems.empty();
  17. }
  18. };
  19.  
  20. template <typename T>
  21. void Stack<T>::push(T const& elem){
  22.  
  23. elems.push_back(elem); // append copy of passed elem
  24. }
  25.  
  26. template<typename T>
  27. T* Stack<T>::pop(){
  28.  
  29.  
  30. if (elems.empty()){
  31.  
  32. throw std::out_of_range("Stack<>::pop(): empty stack");
  33. }
  34.  
  35. T value = elems.back();
  36.  
  37. elems.pop_back();
  38.  
  39. return &value;
  40. }
  41.  
  42. template <typename T>
  43. T Stack<T>::top() const{
  44.  
  45. if (elems.empty()){
  46.  
  47. throw std::out_of_range("Stack<>::top(): empty stack");
  48. }
  49.  
  50. return elems.back(); // return copy of last element
  51. }

this is what I have in my general tree class right now, to show you how I am implementing it

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include "Stack.h"
  4.  
  5. using namespace std;
  6. class Tree{
  7.  
  8. private:
  9.  
  10. //inner node class
  11. class TreeNode{
  12.  
  13. public:
  14.  
  15. //name of node
  16. char name;
  17. //number of children
  18. int children;
  19. //left most child
  20. TreeNode *leftChild;
  21. //right sibling
  22. TreeNode *rightSibling;
  23. //treenode constructor
  24. TreeNode(char nodeName, int numChildren){
  25.  
  26. this->name = nodeName;
  27. this->children = numChildren;
  28. this->leftChild = NULL;
  29. this->rightSibling = NULL;
  30. }
  31. };
  32.  
  33. //top of tree(stack form)
  34. TreeNode *top;
  35.  
  36. public:
  37.  
  38. Tree::Tree(){
  39.  
  40. this->top = makeTree();
  41. }
  42.  
  43. //tree creation
  44. TreeNode* makeTree(){
  45.  
  46. //input file stream
  47. ifstream fin;
  48. //opens the file
  49. fin.open("prog1.txt");
  50. // Verifies file is opened correctly
  51. if(!fin){
  52. cout << "OOPS" << endl;
  53. exit(1);
  54. }
  55.  
  56. char name;
  57. int count;
  58.  
  59. Stack<TreeNode> myStack;
  60. do{
  61.  
  62. fin >> name >> count;
  63.  
  64. TreeNode *newNode = new TreeNode(name, count);
  65. cout << newNode->name << newNode->children << endl;
  66. cout << "here 1" << endl;
  67.  
  68. for(int kid = 0; kid < count; kid++){
  69.  
  70. //TreeNode *child = myStack.pop();
  71. //cout << "here2" << endl;
  72. //child->rightSibling = newNode->leftChild;
  73. //newNode->leftChild = child;
  74. //cout << child->name << child->children << endl;
  75. }
  76.  
  77. myStack.push(*newNode);
  78.  
  79. cout << "also here" << endl;
  80. TreeNode *myNode = myStack.pop();
  81. cout << myNode->name << myNode->children << endl;
  82.  
  83. cout << "here3" << endl;
  84.  
  85. }while(!fin.eof());
  86.  
  87. return myStack.pop();
  88.  
  89. fin.close();
  90. }
  91.  
  92. };
Reputation Points: 10
Solved Threads: 0
Newbie Poster
USUAggie is offline Offline
13 posts
since Jan 2008
Jun 16th, 2008
0

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--

c++ Syntax (Toggle Plain Text)
  1.  
  2. T value = elems.back(); //T value is a temporary object
  3.  
  4. elems.pop_back();
  5.  
  6. return &value; //the object in value fell out of scope before return

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--

c++ Syntax (Toggle Plain Text)
  1.  
  2. T *value = &elems.back(); //T *value is now a pointer that points to the address of
  3. //the object
  4. elems.pop_back();
  5.  
  6. return value; //value should still exist
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 16th, 2008
0

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
USUAggie is offline Offline
13 posts
since Jan 2008
Jun 17th, 2008
0

Re: C++ Stack/GeneralTree help

Click to Expand / Collapse  Quote originally posted by USUAggie ...
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?
Notice that your pop method returns a pointer. Remember that when you make a call such as..

c++ Syntax (Toggle Plain Text)
  1.  
  2. int a = 5; *iPtr = &a;
  3.  
  4. cout << iPtr << endl;

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--

c++ Syntax (Toggle Plain Text)
  1.  
  2. cout << *myStack.pop() << endl;
  3. cout << *myStack.pop();
Last edited by Alex Edwards; Jun 17th, 2008 at 12:19 am.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sorting huge amount of numbers
Next Thread in C++ Forum Timeline: sudoku program 3x3 only





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC