problem in constructing two dimensional array in c++ using classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 30
Reputation: amitahlawat20 is an unknown quantity at this point 
Solved Threads: 2
amitahlawat20's Avatar
amitahlawat20 amitahlawat20 is offline Offline
Light Poster

problem in constructing two dimensional array in c++ using classes

 
0
  #1
Mar 9th, 2008
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!!!
};
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: problem in constructing two dimensional array in c++ using classes

 
0
  #2
Mar 9th, 2008
Please use code tags.

  1. class matrix
  2. {
  3. int **p;
  4. int d1,d2;
  5. public:
  6. matrix(int x,int y); //constructor allocates block of specified size
  7. /*I am not specifying contructor code*/
  8.  
  9.  
  10. void get_element(int i,int j,int value)
  11. {
  12. p[i][j]=value;
  13. }
  14. int put_element(int i,int j)
  15. {
  16. return p[i][j];
  17. } // return statement encountering access violation during runtime , Help!!!
  18. };

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: problem in constructing two dimensional array in c++ using classes

 
0
  #3
Mar 9th, 2008
your constructor is probably wrong
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: problem in constructing two dimensional array in c++ using classes

 
0
  #4
Mar 9th, 2008
  1. int put_element(int i, int j)
  2. {
  3. // check here that both i and j are within the bounds
  4. // you currently have allocated ...
  5. // And remember that indexes are zero-based, i.e.
  6. // if you have 'int arr[2]', the last valid index is 1 (not 2)
  7. return p[i][j];
  8. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC