two-dimensional dynamic array of pointers to classes

This is a discussion on two-dimensional dynamic array of pointers to classes within the C++ Programming forums, part of the General Programming Boards category; Hello, I've got the following problem: I want a two-dimensional dynamic array of pointers to classes . In the two ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    two-dimensional dynamic array of pointers to classes

    Hello, I've got the following problem:

    I want a two-dimensional dynamic array of pointers to classes. In the two dimensional dynamic array will be pointers to another class. So that I can call m_pPointer[x][y] that points to a class.
    I already got this for a one-dimensional dynamic array of pointers to classes. I did that on the next way:

    Code:
    CClassA::CClassA(int row) 
    { 
        int x; 
        m_pPointer = new CClassB*[row]; 
        for(x=0; x<row; x++) 
        { 
            m_pPointer[x] = new CClassB; 
        } 
    }
    With this code I've created an array of pointers to the class CClassB. When row = 10, each m_pPointer[0] to m_pPointer[9] points to a class CClasseB.

    To do this for a two-dimensional array I thought I could do it on this was:
    Code:
    CClassA::CClassA(int row, int col) 
    { 
        int x, y; 
        m_pPointer = new CClassB*[row]; 
        for(x=0; x<row; x++) 
        { 
            m_pPointer[x] = new CClassB[col]; 
            for(y=0; y<col; y++) 
            { 
                m_pPointer[x][y] = new CClassB; 
            } 
        } 
    }
    With this code I hoped that I would have a 2d array with pointers to classes, so that I could say:
    Code:
    cout << m_pPointer[0][0];
    But unfortunately I C++ doesn't think so, I get the following error:
    Code:
    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CClassB' (or there is no acceptable conversion)
    What am I doing wrong?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Well did you overload the << operator to handle your class if not the << does not know how to print out a class.
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Quote Originally Posted by prog-bman
    Well did you overload the << operator to handle your class if not the << does not know how to print out a class.
    Well, this is actually not the problem, but I wanted to indicate that something was going wrong.

    The m_pPointer is declared as follows:
    Code:
    CClassB	**m_pPointer;

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    I've used the following code, and it worked.
    Code:
    CClasseB*** p = new CClasseB**[rows]; 
    for (int i = 0; i != rows; ++i) 
    { 
        p[i] = new CClasseB*[cols]; 
        for (int j = 0; j != cols; ++j) 
        { 
            p[i][j] = new CClasseB; 
        } 
    }
    I've tried this before and it didn't work, probably forgot a pointer anywhere. But now it works, tnx for the advices.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...don't forget to delete the memory in the destructor:

    Deleting:


    Whenever you use new, you should always use delete to free up the memory when you are done using the memory. And, whenever you use 'new []', you need to use 'delete []'. What that means is: when you use "new []" to create an array, then when you delete, you use 'delete []' to delete the whole array. On the other hand, if you just used "new" to dynamically create the memory, then you just use "delete". In addition, the number of new's should equal the number of delete's: if you count the number of times you use new, there should be an equal number of deletes.

    If you look at your example, you used 'new []' once to create the 2d array of pointers p, you used 'new []' for each row, and you used just 'new' for each object in the 2d array. So, to delete all the memory, you need to do this:
    Code:
    for(int i = 0; i < rows; i++)
    {
    	for (int j = 0; j < cols; ++j) 
    	{ 
    		delete p[i][j]; 
                   //no brackets after delete because p[i][j] 
                   //does not point to an array
    	} 
    
    	delete[] p[i]; //[] because p[i] points to an array
    }
    
    delete[] p; //[] because p points to an array
    The reason you need brackets to delete arrays is because a pointer to an array looks like this

    Code:
    className** p-------->className*
                          className*
                          className*
    Last edited by 7stud; 04-21-2005 at 08:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

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