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

Overloading ~ operator at 2x2 matrix (determinant)

P: 1
Hey,
I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the first two are working but I don't know how to overload the ~ one.
here's what I have:
matrix matrix::operator~(matrix &m) {
double x;
x=(m.a11*m.a22)-(m.a12*m.a21);
return x;

of course it's not working cause i can't return a double value in a matrix class. Second of all it says that ~unary operator has too many parameters. But then how do I make a matrix class return only 1 parameter? (since the determinant is only one number).
thanks!
Nov 9 '08 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. matrix matrix::operator~(matrix &m) {
  2. double x;
  3. x=(m.a11*m.a22)-(m.a12*m.a21);
  4. return x;
The determinant is performed on 1 matrix, so you don't need to pass in another matrix. That is, you'll never have to deal with an expression like "mat1 ~ mat2;", but instead, "~mat1". Just deal with the this pointer to get to your elements.

To return a double instead of a matrix, just change the return value. Right now, you've told it you're returning a matrix.
Nov 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.