C++ General Tree

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

Join Date: Jun 2008
Posts: 5
Reputation: didijc is an unknown quantity at this point 
Solved Threads: 0
didijc didijc is offline Offline
Newbie Poster

C++ General Tree

 
0
  #1
Jul 3rd, 2009
Hey C++ guru's...

I'm pretty new to the C++ world and would really appreciate some help... ...

I'm trying to build a general tree (a tree with one root and N children), I've written the code and compiled it quote/un-quote successfully... ...I say that because I am faced with a runtime error that basically stops my efforts dead in its tracks...

Attached to this is a screenshot of the runtime error...

  1. /////// ********* GeneralTree.h *********///////
  2. ///////-------------------------------------///////
  3. #ifndef GENERALTREE_h
  4. #define GENERALTREE_h
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class GeneralTree{
  11. public:
  12. struct GenTreeNode{
  13. int int_transactionID, int_totalNumChildren;
  14. GenTreeNode *ptr_nextChild;
  15. };
  16.  
  17. //initialize root
  18. GenTreeNode *root;
  19.  
  20. GeneralTree(){
  21. int int_totalNumChildren = 0;
  22. }
  23.  
  24. ~GeneralTree(){
  25. clear();
  26. }
  27.  
  28. void clear(){
  29. //point to the node to be deleted
  30. GenTreeNode *tmp;
  31. //used to visit each node in the tree.
  32. //We start with the first actual node off of "root"
  33. GenTreeNode *traverse = root->ptr_nextChild;
  34.  
  35. //while the tree is not empty
  36. while(traverse != NULL){
  37. //store the current node
  38. tmp = traverse;
  39. //visit the next node
  40. traverse = traverse->ptr_nextChild;
  41. //free the memory taken up by the current node
  42. delete tmp;
  43. }
  44. }
  45.  
  46. void addChildren(int *tranID, int cNo){
  47. int int_totalNumChildren = cNo;
  48. GenTreeNode *genTree = new GenTreeNode[int_totalNumChildren];
  49.  
  50. for(int i=0; i<int_totalNumChildren; i++){
  51. genTree->int_transactionID = tranID[i];
  52. }
  53. }
  54.  
  55. void PrintTree(GenTreeNode *tree) {
  56. /* .: Print all the items in the tree to which root points...the item in the root is printed first, followed by its children :: as long as the root is not empty :. */
  57. if (tree != NULL){
  58. cout << tree->int_transactionID << " ::- " << tree->int_totalNumChildren << endl;
  59. // Print children
  60. PrintTree(tree->ptr_nextChild);
  61. }
  62. }
  63.  
  64. void deleteChild(GenTreeNode *ChildPtr){
  65. }
  66. };
  67.  
  68. #endif
  69.  
  70. /////// ********* Main.cpp *********///////
  71. ///////-------------------------------///////
  72. #include <iostream>
  73. #include <fstream>
  74. #include "GeneralTree.h"
  75.  
  76. using namespace std;
  77.  
  78. int main(){
  79. GeneralTree *gTree = new GeneralTree;
  80. int tID = 100;
  81. int numOfChildren = 10;
  82.  
  83. gTree->addChildren(&tID, numOfChildren);
  84. gTree->PrintTree(gTree->root);
  85.  
  86. return 0;
  87. }
Attached Thumbnails
error.jpg  
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: steveballmer is an unknown quantity at this point 
Solved Threads: 0
steveballmer steveballmer is offline Offline
Newbie Poster

Re: C++ General Tree

 
0
  #2
Jul 3rd, 2009
Very good stuff here!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: C++ General Tree

 
0
  #3
Jul 3rd, 2009
Your going to have to provide us with the GeneralTreeNode class please.

------------ edit -----------------
oh its a structure didn't see.. one sec lemme look this over again.
------------ edit -----------------

The only thing i can think of is that you did not declare the integers in the GeneralTreeNode structure, and therefore they are some crazy value giving you an error. But i think if that was the case you would just have gotten a crazy output not an error.

I don't know. maybe someone else knows. Sorry :/
Last edited by u8sand; Jul 3rd, 2009 at 10:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: C++ General Tree

 
0
  #4
Jul 6th, 2009
One of your pointers is not initialized correctly. Take a look at the call stack in Visual Studio when the error occurs. This way you can identify the statement which causes the error.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: C++ General Tree

 
0
  #5
Jul 6th, 2009
I can see problem here.
  1. for(int i=0; i<int_totalNumChildren; i++) {
  2. genTree->int_transactionID = tranID[i];
  3.  
  4. // in main
  5. int tID = 100;
  6. gTree->addChildren(&tID, numOfChildren);
  7.  
  8.  
  9. }
The problem is clear you have passed the address of local variable tID and you are treating it as an array.

what you actually trying to acheive by this?. describe the logic then we'll provide you the most appropriate solution for your case.
Reply With Quote Quick reply to this message  
Reply

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




Views: 301 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC