C++ Unary - Operator Overload Won't Compile -
i attempting create overloaded unary - operator can't code compile. cut-down version of code follows:-
class frag { public: frag myfunc (frag oper1, frag oper2); frag myfunc2 (frag oper1, frag oper2); friend frag operator + (frag &oper1, frag &oper2); frag operator - () { frag f; f.element = -element; return f; } private: int element; }; frag myfunc (frag oper1, frag oper2) { return oper1 + -oper2; } frag myfunc2 (frag oper1, frag oper2) { return oper1 + oper2; } frag operator+ (frag &oper1, frag &oper2) { frag innerfrag; innerfrag.element = oper1.element + oper2.element; return innerfrag; }
the compiler reports...
/home/brian/desktop/frag.hpp: in function ‘frag myfunc(frag, frag)’: /home/brian/desktop/frag.hpp:41: error: no match ‘operator+’ in ‘oper1 + oper2.frag::operator-()’ /home/brian/desktop/frag.hpp:16: note: candidates are: frag operator+(frag&, frag&)
could suggest need doing here?
const-correctness
this has be
frag operator+ (const frag &oper1, const frag &oper2);
or else operands can't temporaries, such return value of operator-
and unary minus should rather be:
frag operator - () const;
since shouldn't modify operand.
Comments
Post a Comment