Path: utzoo!news-server.csri.toronto.edu!bonnie.concordia.ca!ccu.umanitoba.ca!herald.usask.ca!alberta!news From: dwelly@saddle-lk.cs.UAlberta.CA (Andrew Dwelly) Newsgroups: comp.lang.c++ Subject: Beginners problems (longish) Message-ID: <1991Mar13.170912.4020@cs.UAlberta.CA> Date: 13 Mar 91 17:09:12 GMT Sender: news@cs.UAlberta.CA Organization: University of Alberta, Edmonton, Canada Lines: 162 I've just started using c++ and I have an example of code here that I can't get to run, even though it seems to be legal c++. This is almost certainly a beginners mistake, but I can't for the life of me see what the problem is. The situation : I've produced a class of dynamic matrices, with appropriate access, constructor and destructor functions. Now I'm trying to get a simple matrix multiply to work, overloading the * operator. In my main function, I create and print out a matrix "m", then try to print out m*m; What I think I'm doing : I create an automatic matrix variable for my results, then perform a simple multiply on the inputs, placing the result in the output variable. Finally I return "out". Since my function has been declared as returning a matrix, I understood that a _copy_ of "out" was returned before the destructor was called on "out". What actually happens : Compiling with g++ on a Sparc produces the following results :- saddle-lk% Matrix 1 2 3 4 2.24208e-44 2.16332e-40 0 2.16355e-40 Segmentation fault (core dumped) Would someone take pity on me and tell me what I'm doing wrong ??? Andy Dwelly ------------------------------------------------------------------------------- #include #include #include class matrix { float **m; int rows; int cols; public: matrix(int,int); ~matrix(); float*& operator[](int); int sizer(); int sizec(); friend matrix operator*(matrix&, matrix&); }; // We want to be able to print it out ostream& operator<<(ostream& s, matrix& m) // m must be a reference, why ? { for (int i = 0; i < m.sizer(); i++) { for (int j = 0; j < m.sizec(); j++) { s << m[i][j] << " "; } s << "\n"; } return s; } matrix::matrix(int r, int c) { rows = r; cols = c; m = new float*[r]; for (int i = 0; i < r; i++) { m[i] = new float[c]; } } matrix::~matrix() { for (int i = 0; i < rows; i++) { delete m[i]; } delete m; } float*& matrix::operator[](int i) { return m[i]; } matrix::sizer() { return rows; } matrix::sizec() { return cols; } // Problems here ???? matrix operator*(matrix& a, matrix& b) { matrix out(a.sizer(), b.sizec()); if (a.sizec() == b.sizer()) { for (int i = 0; i < a.sizer(); i++) { float *row = a.m[i]; float multsum; for (int j = 0; j < b.sizec(); j++) { multsum = 0.0; for (int k = 0; k < b.sizer(); k++) { multsum += row[k] * b.m[k][j]; } out.m[i][j] = multsum; } } } return out; } main() { matrix m(2,2); m[0][0] = 1.0; m[0][1] = 2.0; m[1][0] = 3.0; m[1][1] = 4.0; cout << m << "\n"; cout << m * m << "\n"; } -- ****************************************************************************** Andy Dwelly : dwelly@cs.uablberta.ca, Tel: 403-492-7591 !@#$%$#, %^&*%, - Listen, who swears ? Christopher Robin has fallen downstairs.