| | |
problem in constructing two dimensional array in c++ using classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!!!
};
{
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!!!
};
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
Please use code tags.
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
C++ Syntax (Toggle Plain Text)
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]; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: SDL_toypiano
- Next Thread: copy a file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






