Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts

    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; 
    }
    The code that I currently have for the operator is:
    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];
        }
    }
    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-
    HTML & 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

  • #2
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Shmoove, aman, jkd anyone??? ...

    -sage-
    HTML & 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

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    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 de-reference 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; 10-24-2005 at 10:26 PM.
    OracleGuy

  • #4
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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
        }
    Since the "<<" operator was overloaded for class functions I was under the impression that this would be the operator used... However after explaining it to me, my instructor made me realize that at this level, the "<<" operator is outputing a double and not a class... Since this is the case, my output was correct, but the formatting was wrong... go figure...

    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 non-constant 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
    }
    Sorry for the wild goose chase...

    Thanks for your help,

    -sage-
    HTML & 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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •