sage45
10-23-2005, 09:06 PM
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:#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;
}
The code that I currently have for the operator is:// 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];
}
}
The problem that I am having is that the first call is initiated and returns the correct information (I think, as it seems the return the memory location for the pointer to the array of doubles) however, the second call is never initiated... What am I doing wrong??? I also know that this is much easier by overloading the "()" operator, however, the class objectives state that we must overload the "[]" operators...
Thanks for any help,
-sage-
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:#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;
}
The code that I currently have for the operator is:// 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];
}
}
The problem that I am having is that the first call is initiated and returns the correct information (I think, as it seems the return the memory location for the pointer to the array of doubles) however, the second call is never initiated... What am I doing wrong??? I also know that this is much easier by overloading the "()" operator, however, the class objectives state that we must overload the "[]" operators...
Thanks for any help,
-sage-