C++ Unary - Operator Overload Won't Compile
- by Brian Hooper
I am attempting to create an overloaded unary - operator but can't get the code to compile. A cut-down version of the code is as 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 for ‘operator+’ in ‘oper1 + oper2.frag::operator-()’
/home/brian/Desktop/frag.hpp:16: note: candidates are: frag operator+(frag&, frag&)
Could anyone suggest what I need to be doing here?
Thanks.