   Class problem
  I have a class that looks like: "Class(val1, val2)"I''ve defined operations for it but there''s one problem, they all need to be on the right hand side.If I multiply by 7 by going "cClass * 7" it multiplies val1 by 7 but val2 stays the same (thats how it''s supposed to work) but if I do "7 * cClass" I get an error saying "no global operator defined which takes type ''class Complex'' (or there is no acceptable conversion)"I''d appreciate any help at all. 
  Binary operators are best implemented as non-member functions, especially if you need commutative mixed-mode arithmetic (class+other, other+class). class Complex{   double re, im;public:   Complex& operator *=( double rhs )   { re *= rhs; im *= rhs; return *this;   }};Complex operator*( double lhs, Complex rhs){  rhs *= lhs;  return rhs;}Complex operator*( Complex lhs, double rhs){  lhs *= rhs;  return lhs;}Note - *= returns by reference, but * must   return by value.Also, if you want a complex number class, consider using the one that is in the C++ library, through <complex> header.[/source]    “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 11, 2004 10:06:37 AM];  I just tried what you suggested but I still get the same error message, I even tried a direct copy and paste but it still says it''s not defined ;  I had forgotten a semicolon at the end of the class declaration. Try again and/or post your code.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  This is my Complex class so far: class Complex{private:float re;float im;  public:Complex();Complex(float r,float i) {re = r; im = i;}~Complex() {};   Complex operator + (Complex b);Complex operator - (Complex b);Complex operator * (Complex b);Complex operator / (Complex b);Complex operator + (float b);Complex operator - (float b);Complex operator * (float b);Complex operator / (float b);Complex operator=(Complex b);   Complex& operator *= (float b){re *= b;im *= b;return *this;}};I also added in the 2 * functions as you had them... ;  Sorry, I didn''t put a prototype in the main file.Thankyou very much, this has been bugging me for a couple of days now With complex numbers you shouldn''t do what you''ve done multiplying the real and the imaginary by the double, you should only do the real? At least thats what I though :-/ ;  Typically, I see commutative operators defined as friends of the class.  Your two multiplies would look like this in the header:class Complex{  ...  public:    ...    friend Complex operator * (float b, const Complex& c);    friend Complex operator * (const Complex& c, float b);    friend Complex operator * (const Complex& c0, const Complex& c1);    ...};  and like this in the source file:Complex operator * (float b, const Complex& c){  ...}Complex operator * (const Complex& c, float b){  ...}Complex operator * (const Complex& c0, const Complex& c1){  ...} ;quote:Original post by Theriomorph  With complex numbers you shouldn''t do what you''ve done multiplying the real and the imaginary by the double, you should only do the real? At least thats what I though :-/   If you''re multiplying by a real number, you multiply both the real and imaginary parts.(a+ib)*(c+id) = (ac-bd)+i(ad+bc)Now, set b to zero, you get a*(c+id)=ac+iad.And, I repeat, as a rule, you should not have your binary operators as member functions. Define your augmented assigment as a member, and have the binary operations be non-members that use them. Alternatively, as Agony has shown, you can make your binary functions to be friend non-members, but that is generally not necessary.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor)