C ++ Unary - operator overloading will not compile

I am trying to create an overloaded unary operator, but I cannot compile it. The cut-out 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; } 

Compiler Report ...

 /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&) 

Can anyone suggest what I need to do here?

+4
source share
4 answers

Const correctness

It should be

  frag operator+ (const frag &oper1, const frag &oper2); 

either operands cannot be temporary, for example, the return value of operator-

And the unary minus should be:

 frag operator - () const; 

since it should not change the operand.

+7
source

You do not have operator+ that can work in time series. Temporary parameters cannot be passed as a non-constant reference.

Change the signature of your operator+ to:

 frag operator + (const frag &oper1, const frag &oper2); 
+3
source

Despite the fact that your question has already been answered fairly well, I think it's worth mentioning another question about your code. You have the following ads right now:

 class frag { public: frag myfunc (frag oper1, frag oper2); frag myfunc2 (frag oper1, frag oper2); 

... and you have the following functions:

 frag myfunc (frag oper1, frag oper2) { return oper1 + -oper2; } frag myfunc2 (frag oper1, frag oper2) { return oper1 + oper2; } 

I would suggest that you use these two functions to implement member functions declared in frag , but they do not. Instead, you have two member functions that are never declared defined, and these two are global functions that have similar names. In order for them to be member functions declared by you, you need to change the declaration to something like:

 frag frag::myfunc(frag oper1, frag oper2) { return oper1 + -oper2; } frag frag::myfunc2(frag oper1, frag oper2) { return oper1 + oper2; } 

On the other hand, this also makes no sense - in particular, as member functions, they are usually called something like: a.myfunc(b,c); They are both written as global functions, although they usually take only one parameter as member functions and use this as the first parameter:

 frag frag::myfunc1(frag oper) { return *this + -oper; } frag frag::myfunc2(frag oper) { return *this + oper; } 

Of course, this can be an accidental side effect of trying to reduce the source code to the minimum necessary for publication. If so, please feel free to ignore this whole "answer" ....

+1
source

The answer has already been given (const arguments), but I would like to mention that Visual C ++ 9 (VS-2008) would really compile this without warning.

0
source

Source: https://habr.com/ru/post/1308137/


All Articles