Hello, I am somewhat new to C++ and trying out some operator overloading. I am getting some errors which I can not understand why. Here are what the errors look like:
1 2 3 4 5
1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(93): error C2556: 'LongInt &operator +(LongInt &,LongInt &)' : overloaded function differs only by return type from 'LongInt operator +(LongInt &,LongInt &)'
1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(32) : see declaration of 'operator +'
1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(93): error C2040: 'operator +' : 'LongInt &(LongInt &,LongInt &)' differs in levels of indirection from 'LongInt (LongInt &,LongInt &)'
If someone could help me fix these, that'd be great. Thanks
You declared operator + to return a LongInt and defined it to return LongInt&. Those are very different things, so C++ can't decide which one you want.
I can guess that you are using a MS VC++ compiler. It is a bug of this compiler! It shall not see the friend function name until it will not be defined. So your code shall be compiled without an error even if the declaration of the friend function does not correspond to the definition of the function with the same name. The friend function and the definition of the other function with the same name are two different functions but the name of the friend function is not visible in the declaration region where the second function is defined.
But nevertheless next time you shall be careful and provide that the declaration of a function corresponds to its definition.