How to add standard and user defined objects?
<p>Hi I am trying to do operator overloading for +(addition) operator to add my user defined datatype objects .
Following is the code for that.</p>

<pre><code>  #include &lt;iostream&gt;
using namespace std;
class complex {
    int i;
    double f;
public:
    complex(int ii=0,double ff=0){
        i = ii;
        f = ff;
    }
    complex operator+(complex object) {
        complex result;
        result.i = this-&gt;i + object.i;
        result.f = this-&gt;f + object.f;
        return result;
    }
    void display() {
        cout &lt;&lt; i &lt;&lt;"\t"&lt;&lt; f;
        cout &lt;&lt; endl;
    }
};
int main(){
    complex obj1(1,1.1),obj2(2,2.2),obj3;
    int i(5);
    obj3 = obj1 + obj2;
    obj3.display();
    obj3 = obj3 + i;
    obj3.display();
    obj3 = i + obj3;//generates me compiler error
    obj3.display();
    return 0;
}
</code></pre>

<p>I have learnt that when I do obj1 + obj2,it is expanded by the compiler as obj1.operator+(obj2);
So that part of code works fine.</p>

<p>But when I do add an int and complex ,I think it get expanded as i.operator(obj1).</p>

<p>So it gives me compiler errors.</p>

<p>Should I define operator+ function in int class or how to solve this?
Please suggest,
Thank you,</p>
