Home › Forums › C Programming › cin, cout, matrix, pointers and references :(
- This topic has 1 reply, 2 voices, and was last updated 16 years, 9 months ago by Humayan.
- AuthorPosts
- March 20, 2008 at 2:20 am #2081DeepakParticipant
i am working with a matrix manipulation program…consists of a matrix class and its member functions..i have also overloaded << and >>…so dat dey can read and print d whole matrix at one statement..the code of these overloaded operators is something like this..
123456789101112131415161718192021222324<br />// for >> (cin) :<br />istream& operator >> (istream &read, matrix &mat)<br />{<br />for (int i = 0; i < mat.rows * mat.columns; ++i)<br />read >> *(mat.element+i);<br />return read;<br />}<br /><br />// for << (cout) :<br />ostream& operator << (ostream &print, matrix &mat)<br />{<br />for (int i = 0; i < mat.rows; ++i)<br />{<br />for (int j = 0; j < mat.columns; ++j)<br />{<br />print << *mat.element << " ";<br />++mat.element;<br />}<br />print << endl;<br />}<br />mat.element -= mat.rows * mat.columns;<br />return print;<br />}here i have received the variable matrix1 by reference…in main() i have used a variable matrix1 which is passed as follows :matrix matrix1;cin >> matrix1;now…my question is : wat if i want to pass a pointer variable which should be received by a reference…smthing like this…
1234567<br />main()<br />{<br />matrix *matrix2;<br />cin >> matrix2; //i aint sure that this statement is right...it may be cin >> *matrix2...<br />}<br />the overloaded function for pointer thing is coded as follows :
12345678910111213141516171819202122232425for >> :<br />istream& operator >> (istream &read, matrix *&mat)<br />{<br />for (int i = 0; i < mat->rows * mat->columns; ++i)<br />read >> *(mat->element+i);<br />return read;<br />}<br /><br />// for << :<br />ostream& operator << (ostream &print, matrix *&mat)<br />{<br /><br />for (int i = 0; i < mat->rows; ++i)<br />{<br />for (int j = 0; j < mat->columns; ++j)<br />{<br />print << *mat->element << " ";<br />++mat->element;<br />}<br />print << endl;<br />}<br />mat->element -= mat->rows * mat->columns;<br />return print;<br />}<br />however my problem is when i execute the program…then while taking input for a pointer matrix…it just reads one value instead of all the values..but doesnt even print that single value..i think there must be bug in those overloaded functions used for pointer variable.. :(so please check out the functions and post the solution or your suggestions…
- March 20, 2008 at 10:46 pm #3367HumayanParticipant
Can you post the whole code?
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.