| | |
C++ General Tree
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
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...
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)
/////// ********* GeneralTree.h *********/////// ///////-------------------------------------/////// #ifndef GENERALTREE_h #define GENERALTREE_h #include <iostream> using namespace std; class GeneralTree{ public: struct GenTreeNode{ int int_transactionID, int_totalNumChildren; GenTreeNode *ptr_nextChild; }; //initialize root GenTreeNode *root; GeneralTree(){ int int_totalNumChildren = 0; } ~GeneralTree(){ clear(); } void clear(){ //point to the node to be deleted GenTreeNode *tmp; //used to visit each node in the tree. //We start with the first actual node off of "root" GenTreeNode *traverse = root->ptr_nextChild; //while the tree is not empty while(traverse != NULL){ //store the current node tmp = traverse; //visit the next node traverse = traverse->ptr_nextChild; //free the memory taken up by the current node delete tmp; } } void addChildren(int *tranID, int cNo){ int int_totalNumChildren = cNo; GenTreeNode *genTree = new GenTreeNode[int_totalNumChildren]; for(int i=0; i<int_totalNumChildren; i++){ genTree->int_transactionID = tranID[i]; } } void PrintTree(GenTreeNode *tree) { /* .: 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 :. */ if (tree != NULL){ cout << tree->int_transactionID << " ::- " << tree->int_totalNumChildren << endl; // Print children PrintTree(tree->ptr_nextChild); } } void deleteChild(GenTreeNode *ChildPtr){ } }; #endif /////// ********* Main.cpp *********/////// ///////-------------------------------/////// #include <iostream> #include <fstream> #include "GeneralTree.h" using namespace std; int main(){ GeneralTree *gTree = new GeneralTree; int tID = 100; int numOfChildren = 10; gTree->addChildren(&tID, numOfChildren); gTree->PrintTree(gTree->root); return 0; }
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 :/
------------ 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.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
I can see problem here.
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.
C++ Syntax (Toggle Plain Text)
for(int i=0; i<int_totalNumChildren; i++) { genTree->int_transactionID = tranID[i]; // in main int tID = 100; gTree->addChildren(&tID, numOfChildren); }
what you actually trying to acheive by this?. describe the logic then we'll provide you the most appropriate solution for your case.
![]() |
Similar Threads
- General Tree - Mirror Image (C++)
- C++ Stack/GeneralTree help (C++)
- "Trees" assignment... (C)
- parsing a tree (C)
- Run-time Error when printing Array Contents. (C)
Other Threads in the C++ Forum
- Previous Thread: How to call a class member function via function pointer in map [C++]
- Next Thread: FILE writer help
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





