943,988 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 916
  • C++ RSS
Jul 3rd, 2009
0

C++ General Tree

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

C++ Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	error.jpg
Views:	38
Size:	89.1 KB
ID:	10709  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
didijc is offline Offline
15 posts
since Jun 2008
Jul 3rd, 2009
0

Re: C++ General Tree

Very good stuff here!
Reputation Points: 9
Solved Threads: 0
Newbie Poster
steveballmer is offline Offline
3 posts
since Nov 2008
Jul 3rd, 2009
0

Re: C++ General Tree

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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 6th, 2009
0

Re: C++ General Tree

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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Jul 6th, 2009
0

Re: C++ General Tree

I can see problem here.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006

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: How to call a class member function via function pointer in map [C++]
Next Thread in C++ Forum Timeline: FILE writer help





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


Follow us on Twitter


© 2011 DaniWeb® LLC