C++ Stack/GeneralTree help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 13
Reputation: USUAggie is an unknown quantity at this point 
Solved Threads: 0
USUAggie USUAggie is offline Offline
Newbie Poster

C++ Stack/GeneralTree help

 
0
  #1
Jun 16th, 2008
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?



  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

  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. };
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: C++ Stack/GeneralTree help

 
0
  #2
Jun 16th, 2008
You're asking to return a pointer when you're really returning the address of an object that has fallen out of scope--

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

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 13
Reputation: USUAggie is an unknown quantity at this point 
Solved Threads: 0
USUAggie USUAggie is offline Offline
Newbie Poster

Re: C++ Stack/GeneralTree help

 
0
  #3
Jun 16th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: C++ Stack/GeneralTree help

 
0
  #4
Jun 17th, 2008
Originally Posted by USUAggie View Post
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..

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

  1.  
  2. cout << *myStack.pop() << endl;
  3. cout << *myStack.pop();
Last edited by Alex Edwards; Jun 17th, 2008 at 12:19 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC