Resizing Arrays in a Class

This is a discussion on Resizing Arrays in a Class within the C++ Programming forums, part of the General Programming Boards category; This is what I am using right now (doesn't work). The AddLeaf function only works the first time thru, when ...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Resizing Arrays in a Class

    This is what I am using right now (doesn't work). The AddLeaf function only works the first time thru, when there are 0 nodes.
    PHP Code:
    class NodeTree
    {
    public:
        
    NodeTree();        // Constructor
        
    ~NodeTree();    // Deconstructor
        
    bool AddNode(Node *LeftNode *Rightlong Tag);
        
    bool AddLeaf(char Characterlong Tag);
    protected:
        
    long NodeCount;
        
    Node *TheNodes;
    };

    ...

    bool NodeTree::AddLeaf(char Characterlong Tag)
    {
        
    int i 0;
        
    Node *TempNodes = new Node[NodeCount];
        for(
    0NodeCounti++)
        {
            
    TempNodes[i] = TheNodes[i]; //Error!
        
    }
        
    delete[] TheNodes;    
        
    Node *TheNodes = new Node[NodeCount 1];
        for(
    0NodeCounti++)
        {
            
    TheNodes[i] = TempNodes[i];
        }
        
    delete[] TempNodes;
        
    TempNodes NULL;    
        
    TheNodes[NodeCount].Character Character;
        
    TheNodes[NodeCount].Type Leaf;
        
    TheNodes[NodeCount].Left NULL;
        
    TheNodes[NodeCount].Right NULL;
        
    TheNodes[NodeCount].Tag Tag;
        
    NodeCount++;
        return 
    true;


  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I saw something that was redundant in your code.

    -----
    // Node *TheNodes = new Node[NodeCount + 1];

    // TheNodes is declared in class declaration
    // this will work fine

    TheNodes = new Node[NodeCount + 1];
    -----

    What is NodeCount initialized to?

    Kuphryn

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    try changing you assigning for statement to this:
    Code:
    *TheNodes[i] = *TempNodes[i];
    It looks like right now you were assigning memory addresses and not values.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by kuphryn
    I saw something that was redundant in your code.

    -----
    // Node *TheNodes = new Node[NodeCount + 1];

    // TheNodes is declared in class declaration
    // this will work fine

    TheNodes = new Node[NodeCount + 1];
    -----
    Thanx, kuphryn that worked perfectly! Initially, NodeCount is 0, btw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  3. Replies: 1
    Last Post: 04-20-2003, 05:02 PM
  4. arrays as class names
    By Katle in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2002, 02:58 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21