Operator Overloading Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xkenneth

    #1

    Operator Overloading Problems

    Hi,

    I'm writing a sparse matrix class for class and I cannot seem to get
    operator overloading to work properly.

    I've overloaded an operator with the code here.

    matrix operator +(matrix one, matrix other) {
    cout << "adding" << endl;
    return matrix(NULL,2,2 ); //doesn't actually carry out the operation,
    but returns a different value, i'm just trying to test if it works
    }

    And i'm calling it with such a method:

    matrix res(NULL,1,1);
    res = *matrix1 + *matrix2; //this doesn't work
    (*matrix1 + *matrix2).displ ay() //this does? //it displays what i'm
    looking for
    res.display() //this displays the original value of res

    With matrix1 and matrix2 of course being pointers to matrix objects.
    Now, however after the call to the overloaded operator, res has the
    same value, but when I call (*matrix1 + *matrix2) it displays the
    added value i'm looking for. I can't quite understand for the life of
    me why this value doesn't get assigned to the res variable.

    Thanks for your time.

    P.S. What's with all the spam on this newsgroup?

  • Kira Yamato

    #2
    Re: Operator Overloading Problems

    On 2007-11-18 15:39:38 -0500, xkenneth <xkenneth@gmail .comsaid:
    Hi,
    >
    I'm writing a sparse matrix class for class and I cannot seem to get
    operator overloading to work properly.
    >
    I've overloaded an operator with the code here.
    >
    matrix operator +(matrix one, matrix other) {
    cout << "adding" << endl;
    return matrix(NULL,2,2 ); //doesn't actually carry out the operation,
    but returns a different value, i'm just trying to test if it works
    }
    >
    And i'm calling it with such a method:
    >
    matrix res(NULL,1,1);
    res = *matrix1 + *matrix2; //this doesn't work
    (*matrix1 + *matrix2).displ ay() //this does? //it displays what i'm
    looking for
    res.display() //this displays the original value of res
    >
    With matrix1 and matrix2 of course being pointers to matrix objects.
    Now, however after the call to the overloaded operator, res has the
    same value, but when I call (*matrix1 + *matrix2) it displays the
    added value i'm looking for.
    This suggest the problem maybe with the assignment operator. Did you
    actually defined one? Can we see it?
    I can't quite understand for the life of
    me why this value doesn't get assigned to the res variable.
    >
    Thanks for your time.
    >
    P.S. What's with all the spam on this newsgroup?
    The best way to fight spam is just to ignore it. Don't talk about it,
    don't ask about it. Just pretend they're not here at the first place.
    So, it seems like I just broke my own rule here, but you don't have to
    make it worse by commenting back on this point. Just ignore it.

    --

    -kira

    Comment

    Working...