Thread: overload contructors || check bounds of array

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    overload contructors || check bounds of array

    i need to overload a constructor to specify dimensions in a 2-d array and check to see if its within the bounds of 10x10. and i keep getting errors on my attempt. for clarity ill just include the whole class.
    Code:
    class matrix
    {
    
    private:
            int lim1,lim2;
            int mtx[lim1][lim2];
    
    public:
            matrix():       lim1(10),lim2(10)
            {       }
            matrix(int x,int y): lim1(x),lim2(y)    //RESTRICT defined as 10
            {
                    if(x < RESTRICT-1 || y < RESTRICT-1)
                    {
                            lim1 = x;
                            lim2 = y;
                    }
                    else
                            cout << "Error:  specified array too large." << endl;
            }
            void putel(int index1,int index2,int value)
            {
                    if(index1 <= RESTRICT-1 && index2 <= RESTRICT-1)
                            mtx[index1][index2] = value;
                    else
                            cout << "Error: exceeded limits of array" << endl;
            }
    
            int getel(int index1,int index2)
            {
                    if(index1 <= RESTRICT-1 && index2 <= RESTRICT-1)
                    {
                            int tmp = mtx[index1][index2];
                            return tmp;
                    }
                    else
                            cout << "Error: exceeded limits of array" << endl;
            }
    };
    //errors
    ch7_ex10.cpp:11: error: invalid use of member `matrix::lim1'
    ch7_ex10.cpp:11: error: invalid use of member `matrix::lim2'
    ch7_ex10.cpp: In member function `void matrix::putel(int, int, int)':
    ch7_ex10.cpp:29: error: `mtx' undeclared (first use this function)
    ch7_ex10.cpp:29: error: (Each undeclared identifier is reported only once for 
       each function it appears in.)
    cant figure out why it says invalid use of member...help much appreciated. thanks in advance

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    >int mtx[lim1][lim2];

    You cant say that because lim1 and lim2 are undefined.

    You have to say:

    int mtx[][];

    OR:

    int **mtx;

    AFTER lim1 and lim2 become defined, THEN you can resize the matrix as such:

    mtx = new int[lim1][];
    for ( int k = 0; k < lim1; k++ )
    {
    mtx[k] = new int[lim2];
    }
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks. deleting lim1 and lim2 worked. before all this i tried setting lim1 = lim2 = 10. but somehow that didnt work. oh well..fixed now.
    i hate arrays

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 08:06 AM
  3. Error
    By Kaho in forum C Programming
    Replies: 5
    Last Post: 08-19-2005, 01:56 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. can i dynamicaly check the size of an array?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 09:12 AM