DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   problem in constructing two dimensional array in c++ using classes (http://www.daniweb.com/forums/thread112923.html)

amitahlawat20 Mar 9th, 2008 3:02 am
problem in constructing two dimensional array in c++ using classes
 
class matrix
{
int **p;
int d1,d2;
public:
matrix(int x,int y); //constructor allocates block of specified size
/*I am not specifying contructor code*/
void get_element(int i,int j,int value)
{ p[i][j]=value; }
int put_element(int i,int j)
{ return p[i][j]; } // return statement encountering access violation during runtime , Help!!!
};

VernonDozier Mar 9th, 2008 6:22 am
Re: problem in constructing two dimensional array in c++ using classes
 
Please use code tags.

class matrix
{
        int **p;
        int d1,d2;
public:
        matrix(int x,int y); //constructor allocates block of specified size
        /*I am not specifying contructor code*/


        void get_element(int i,int j,int value)
        {
                p[i][j]=value;
        }
        int put_element(int i,int j)
        {
                return p[i][j];
        } // return statement encountering access violation during runtime , Help!!!
};

You're going to have to post more code unless someone else sees something wrong with what you posted. I don't. I'm guessing the problem is in your constructor.

bugmenot Mar 9th, 2008 8:23 am
Re: problem in constructing two dimensional array in c++ using classes
 
your constructor is probably wrong

mitrmkar Mar 9th, 2008 11:10 am
Re: problem in constructing two dimensional array in c++ using classes
 
int put_element(int i, int j)
{
    // check here that both i and j are within the bounds
    // you currently have allocated ...
    // And remember that indexes are zero-based, i.e.
    // if you have 'int arr[2]', the last valid index is 1 (not 2)
    return p[i][j];
}


All times are GMT -4. The time now is 3:20 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC