Whats wrong with my code shown below? please somebody throw some light. Thanks for your time !
#include<iostream.h>
using namespace std;
struct mydata{
int mx;
mydata(int x = 0){}
mydata operator+(const mydata& rhs){
mydata temp(rhs);
return temp;
}
operator int() const{ return mx; }
operator double() const{ return mx; }
};
int main(){
mydata d;
mydata r = d + 5; // L1
5 + d; // L2
d + d; // L3
}
The problem (stated the comment) is that compiler doesn’t know which + you want to execute:
or
In order to resolve this ambiguoity, you should point the type conversion, or replace one of these operators by a named function:
If you want instead use d + mydata(5) you should write so, because the above variants are more likely to be applied