return 2d array to caller from public class function

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

Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

return 2d array to caller from public class function

 
0
  #1
May 14th, 2008
Hi, I am having a problem with returning a 2d array back to the main in my program. The program reads in a matrix from a file, then it has to multiply etc, however i cannot get the print function to work since the array that is made in the load function is passed back to the main. Here is my code, i am being told that the array i want to print is undefined. I cannot change any of the public member functions, however i can add to the private, but i dont see how that would help me print.

matrix.h
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. #ifndef _matrix_H_
  6. #define _matrix_H_
  7.  
  8. class matrix
  9. {
  10. public:
  11. matrix();// constructor
  12. matrix(const matrix&);// copy constructor
  13. ~matrix();// destructor
  14.  
  15. bool multiply(const matrix&, const matrix&);
  16. bool isequal(const matrix&);
  17. bool copy(matrix&);
  18. bool load(istream&);
  19. void print(ostream&);
  20. bool isset() const;
  21. void getrowscols(int&, int&) const;
  22. private:
  23. int rows;// number of rows
  24. int cols;// no of columns
  25. float** data;// 2 dimensional array
  26. };
  27.  
  28. #endif
main.cpp
  1. #include <iostream>
  2. #include <fstream>
  3. #include "matrix.h"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char filename[50];
  9.  
  10. matrix* ob;
  11. ob = new matrix();
  12.  
  13. matrix ob2;
  14.  
  15.  
  16. cout << "Enter filename to open: " << endl;
  17. cin >> filename;
  18.  
  19. fstream ins;
  20. ins.open(filename, ios::in | ios::out);
  21.  
  22. ob2.load(ins);
  23. ob2.print(ins);
  24.  
  25. ins.close();
  26.  
  27. return 0;
  28. }
matrix.cpp
  1. #include <iostream>
  2. #include <fstream>
  3. #include "matrix.h"
  4. using namespace std;
  5.  
  6. matrix::matrix()
  7. {
  8. rows = 0;
  9. cols = 0;
  10.  
  11. data = new float *[rows];
  12. for(int i = 0; i < rows; i++)
  13. *(data+i) = new float [cols];
  14. }
  15.  
  16. matrix::matrix(const matrix& p)
  17. {
  18. rows = p.rows;
  19. cols = p.cols;
  20.  
  21. data = new float *[rows];
  22. for(int j = 0; j < rows; j++)
  23. *(data+j) = new float [cols];
  24.  
  25. for(int i = 0; i < rows; i++)
  26. for(int j = 0; j < cols; j++)
  27. *(*(data+i)+j) = *(*(p.data+i)+j);
  28. }
  29.  
  30. matrix::~matrix()
  31. {
  32. for(int i = 0; i < rows; i++)
  33. delete [] *(data+i);
  34. delete [] data;
  35. }
  36.  
  37. bool matrix::load(istream& ins)
  38. {
  39. ins >> rows >> cols; //read in rows and columns
  40.  
  41. float** array = new float*[rows]; //create matrix
  42.  
  43. if (array != NULL)
  44. {
  45. for (int i = 0; i < rows; i++)
  46. {
  47. array[i] = new float[cols];
  48. }
  49. }
  50.  
  51. for (int i = 0; i < rows; i++) //read in data
  52. {
  53. for (int j = 0; j < cols; j++)
  54. {
  55. ins >> array[i][j];
  56. }
  57. }
  58. return array;
  59. return true;
  60. }
  61.  
  62. void matrix::print(ostream& ins)
  63. {
  64. for(int i = 0; i < rows; i++) //print function
  65. {
  66. for(int j = 0; j < cols; j++)
  67. {
  68. ins << " " << array[i][j];
  69. }
  70. cout << endl;
  71. }
  72.  
  73. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,897
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: return 2d array to caller from public class function

 
0
  #2
May 14th, 2008
  1. bool matrix::load(istream& ins)
  2. {
  3. [....more code....]
  4. return array;
  5. return true;
  6. }
You can't return array because the function can only return a bool. So it will return 'true' because the memory-address of array != 0

Why don't you just use the private float** data; to store the data in matrix::load?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: return 2d array to caller from public class function

 
0
  #3
May 14th, 2008
when i use the private float** data i get a segementation fault.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,897
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: return 2d array to caller from public class function

 
0
  #4
May 14th, 2008
That's probably because of this:
  1. matrix::matrix()
  2. {
  3. rows = 0;
  4. cols = 0;
  5.  
  6. data = new float *[rows];
Since there aren't any rows (rows = 0) you can't store data in it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: return 2d array to caller from public class function

 
0
  #5
May 14th, 2008
  1. bool matrix::load(istream& ins)
  2. {
  3. ins >> rows >> cols;
  4. ...

I read in the size of the matrix from the file
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: return 2d array to caller from public class function

 
0
  #6
May 14th, 2008
Thanx for the help, i got it to work using data.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,897
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: return 2d array to caller from public class function

 
0
  #7
May 14th, 2008
[edit] Great!
Last edited by niek_e; May 14th, 2008 at 8:10 am.
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