Operator Overloading Problems

X

xkenneth

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).display() //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?
 
K

Kira Yamato

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).display() //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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top