Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4
Thread: Operator Overloading  C++

10232005, 10:06 PM #1
Operator Overloading  C++
I am having a problem overloading the "[]" operator in a C++ program in school...
Basically I am building a Matrix class and one of the objectives is to overload the subscript operators so that one could essentially access the data in the location specified:Code:#include <iostream> #include <fstream> #include <string> #include "matrix.h" using namespace std; int main() { ifstream fin; ofstream fout; string filename; int rowIdx, colIdx; Matrix A(6, 6); cout << "Enter name of input file: "; cin >> filename; if (OpenInputOrDie(fin, filename)) cout << "\tFile opened." << endl; cout << "Enter name of ouput file: "; cin >> filename; if (OpenOutputOrDie(fout, filename)) cout << "\tFile opened." << endl; fout << "Testing Input, reading new values for A:\n"; fin >> A; fout << "A=\n"<< A << "\n"; fout << "Testing subscripts, 1st 2 rows of A are: \n"; for (rowIdx = 0; rowIdx < A.NRows(); rowIdx++) { for (colIdx = 0; colIdx < A.NCols(); colIdx++) fout << A[rowIdx][colIdx] << '\t'; } fout << "Testing complete."; fout.close(); fin.close(); return 0; }
Code:// In class double* operator [](int idx); double operator [](int idx) const; // In Implementation double *Matrix::operator [](int idx) { if((idx >= 0) && (idx < mRows)) { return mData[idx]; } } double Matrix::operator [](int idx) const { if((idx >= 0) && (idx < mCols)) { return *mData[idx]; } }
Thanks for any help,
sageHTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000

10242005, 07:10 PM #2
Shmoove, aman, jkd anyone??? ...
sageHTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000

10242005, 11:24 PM #3
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,073
 Thanks
 1
 Thanked 328 Times in 324 Posts
Well operator overloading in C++ isn't exactly my strongest area but I'll give it a shot.
Two things I noticed:
Code:// In Implementation double *Matrix::operator [](int idx) { if((idx >= 0) && (idx < mRows)) { return mData[idx]; } } double Matrix::operator [](int idx) //const  Try removing this and giving it a whirl, but this is just a guess. { if((idx >= 0) && (idx < mCols)) { return *(mData[idx]); //If you leave the parentheses out, it will dereference the mData then dereference it against based on the idx value and that might not be what you want to happen. } }
Last edited by oracleguy; 10242005 at 11:26 PM.
OracleGuy

10252005, 12:55 AM #4
Actually I spoke with my instructor after this and found out that there was nothing wrong with my code at all. The problem was with this right here:
Code:for (rowIdx = 0; rowIdx < A.NRows(); rowIdx++) { for (colIdx = 0; colIdx < A.NCols(); colIdx++) fout << A[rowIdx][colIdx] << '\t'; \\this uses the '<<' operator for a double not the class }
Anywho, I also found out that my way, although it works, has the possibility of overwritting the data in the array location. For correctness, I need to use the following:Code://In class // overloaded for nonconstant arrays double* &operator [](int idx) // overloaded for constant arrays const double* &operator [](int idx) const // In implementation //overloaded for nonconstant arrays double* &Matrix::operator[](int index) { assert(0 <= index && index < arraySize); return(list[index]); //return a pointer of the array component } //overloaded for constant arrays const double* &Matrix::operator[](int index) const { assert(0 <= index && index < arraySize); return(list[index]); //return a pointer of the array component }
Thanks for your help,
sageHTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000