Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2005, 09:06 PM   PM User | #1
sage45
Super Moderator


 
Join Date: May 2002
Posts: 1,036
Thanks: 0
Thanked 11 Times in 11 Posts
sage45 is an unknown quantity at this point
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
sage45 is offline   Reply With Quote
Old 10-24-2005, 06:10 PM   PM User | #2
sage45
Super Moderator


 
Join Date: May 2002
Posts: 1,036
Thanks: 0
Thanked 11 Times in 11 Posts
sage45 is an unknown quantity at this point
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
sage45 is offline   Reply With Quote
Old 10-24-2005, 10:24 PM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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.
    }
}
__________________
OracleGuy

Last edited by oracleguy; 10-24-2005 at 10:26 PM..
oracleguy is offline   Reply With Quote
Old 10-24-2005, 11:55 PM   PM User | #4
sage45
Super Moderator


 
Join Date: May 2002
Posts: 1,036
Thanks: 0
Thanked 11 Times in 11 Posts
sage45 is an unknown quantity at this point
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
sage45 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:04 AM.


Advertisement
Log in to turn off these ads.