C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,477 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,748 people online right now. Registration is fast and FREE... Join Now!




General Tree in C++

 

General Tree in C++

didijc

3 Jul, 2009 - 07:40 AM
Post #1

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 1

Good morning everyone...

I'm hoping someone can see what it is I'm doing wrong here -- basically the code complies however I get a run time error and I'm no sure why....

help, please...

CODE

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


CODE

/////// ********* Main.cpp *********///////
///////-------------------------------///////
#include <iostream>
#include <fstream>
#include "GeneralTree.h"

using namespace std;

int main(){
    GeneralTree *gTree = new GeneralTree;
    int tID = 100;
    int numOfChildren = 10;

    cfisTree->addChildren(&tID, numOfChildren);
    cfisTree->PrintTree(gTree->root);

    return 0;
}


User is offlineProfile CardPM
+Quote Post


NickDMax

RE: General Tree In C++

3 Jul, 2009 - 07:51 AM
Post #2

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,216



Thanked: 285 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
What runtime error do you get... any indication of where it occurs?

We are not psychic smile.gif we need evidence to evaluate.

This might be a nice time to learn some debugging techniques. You can use causal output messages to tell where the code is which can often help narrow down where your error occurs. You can use a debugger to step though the code and analyise what it is actually doing.

Your problem is PROBABLY that you try to delete a null pointer OR you try to access memory that you do now have access to. But without something to go on it is hard for us to help you narrow it down.

looks like you never initialize root...
User is offlineProfile CardPM
+Quote Post

Dantheman

RE: General Tree In C++

3 Jul, 2009 - 07:55 AM
Post #3

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
You should value situations like these, because debugging programs is a part of programming. You MUST know it. Why not start now?

I wouldn't have learned even a half of what I know if I were running and asking people to fix my code everytime.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 03:33AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month