2d Array access by other classes

This is a discussion on 2d Array access by other classes within the C++ Programming forums, part of the General Programming Boards category; In my program I have a number of classes. In one of these a 2D array of structs is created ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question 2d Array access by other classes

    In my program I have a number of classes. In one of these a 2D array of structs is created using

    array = new Variables *[rows];
    for (int j = 0; j < rows; j++)
    {
    array[j] = new Variables[cols];
    }

    In one of my other classes one of my methods is querying the array to get a bool value from it using:
    // accessing the constructor of the array class
    Array arrayCells;

    for (int i=0; i < nRows; i++)
    {
    for (int j=0; j < nCols; j++)
    {
    bool cell = arrayCells.checkCellStatus(i,j);
    }
    }

    However it always returns a false bool value for the whole array even though I know they aren't all false.

    I think I might need to pass the array to the class thats trying to access it through a constructor but when i tried that the compiler says that it cant find matching function to the structure I declared in the array class

    Im new to c++ programming and seem to have lost my way any help that some one can give to show me the way back would be much appreciated.

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    You need to input the object's pointer to the function that uses it.

    f.ex. (very simplified)
    Code:
    int main(void)
    {
      Array NewArray;
      CheckCells(&NewArray);  // passes the object's pointer to the function
    }
    // --- or ---
    int main(void)
    {
      Array* pNewArray = new Array;
      CheckCells(pNewArray);  // also passes the pointer
      delete pNewArray;
    }
    
    
    
    bool CheckCells(Array* pArray)
    {
      for (int i=0; i < nRows; i++) 
        for (int j=0; j < nCols; j++) 
          bool cell = arrayCells.checkCellStatus(i,j); 
    }
    I hope this helps
    Making error is human, but for messing things thoroughly it takes a computer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 04:50 PM
  2. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 12:43 PM
  3. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 12:57 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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