By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Operator Overloading Problems

P: n/a
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?

Nov 18 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 2007-11-18 15:39:38 -0500, xkenneth <xk******@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).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.

--

-kira

Nov 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.