operator overloading

can we use only one argument in operation overloading function as a parameter!
here is error of this code
1.‘com com::operator+(com, com)’ must take either zero or one argument|
2.extra qualification com::’ on member ‘operator+’ [-fpermissive]|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

     class com{
     double x,y;
     public:
     void getdata(double,double);
     void display(com);
     com com::operator +(com c,com d);
     };


    void com::getdata (double a,double b){
    x=a;
    y=b;
    }

    void com::display(com c){
    cout<<c.x<<"+i"<<c.y<<endl;
    }

    com com::operator +(com c,com d){
    com finl;
    finl.x=c.x+d.x;
    finl.y=c.y+d.y;
    return(finl);
    }


int main()
{

    com c1,c2,c3;
    c1.getdata(2.3,3.4);
    c2.getdata(5.2,4.4);
    c3=+(c1,c2);
    display(c3);
    return 0;
}
Last edited on
Extra qualification: com:: is not needed to be written when you are inside the com class.
1
2
3
4
5
6
7
     class com{
     double x,y;
     public:
     void getdata(double,double);
     void display(com);
     com operator +(com c,com d);
     };


You can overload operator+ in two ways: internally to the com class (member function), or externally (global function).

If you overload it internally, it will look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     class com{
     double x,y;
     public:
     void getdata(double,double);
     void display(com);
     com operator +(com b);
     };

com com::operator + (com b)
{
    com fin;

    // notice that we use our current x and y
    fin.x = x + b.x;
    fin.y = y + b.y;
    return fin;
}


If you overload it externally, you do it exactly as you did already, but do not declare it in your com class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     class com{
     double x,y;
     public:
     void getdata(double,double);
     void display(com);
     friend com operator +(com c,com d);
    // a friend function can access private members, in our case x and y
     };

    // no com:: this time because it's not part of the com class!
    com operator +(com c,com d){
    com finl;
    finl.x=c.x+d.x;
    finl.y=c.y+d.y;
    return(finl);
    }


I'll stop here because it seems you're a beginner, but you should also look into "const correctness".
http://www.parashift.com/c++-faq/const-correctness.html
Last edited on
what you are saying i got that but my main doubt is that can we use more than one argument in my code in 9th line like this
com operator +(com c,com d);
it is again giving error like ----.‘com com::operator+(com, com)’ must take either zero or one argument|
can we use more than one argument in my code in 9th line

No. Not unless you make it a global overload.

See this table:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Arithmetic_operators
closed account (28poGNh0)
Do you look for something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# include <iostream>
using namespace std;

class com
{
    double x,y;
    public:
        void getdata(double,double);
        void display(void);// This function does not necessirly need that argument
        friend com operator +(com c,com d);
        com operator =(com sum);
};


void com::getdata (double a,double b)
{
    x=a;
    y=b;
}

void com::display(void)
{
    cout << x << "+i" << y << endl;
}

com operator +(com c,com d)
{
    com finl;
    finl.x = c.x + d.x;
    finl.y = c.y + d.y;

    return(finl);
}

com com::operator =(com sum)
{
    x = sum.x;
    y = sum.y;

    return(*this);
}

int main()
{
    com c1,c2,c3;
    c1.getdata(2.3,3.4);
    c2.getdata(5.2,4.4);
    c3=c1+c2;
    c3.display();
    return 0;
}
If you are coding a member function, then you always have a hidden this parameters that refers to the calling object

So, supposing that `+' is a member function
1
2
3
4
5
6
7
8
a.operator+(b);
a + b; //takes one parameter, `b'

a.operator+();
+ a; //takes zero parameters


a.operator+(b,c); //¿does this make sense to you? 


The third snip in Catfish's code shows a non-member function, so no this pointer
1
2
operator+(a,b);
a + b; //takes two parameters, `a' and `b' 



PS: ¿`getdata()' sets data?
Last edited on
@Tech01
no i don't want to use friend function i wanna use that like member function!
closed account (28poGNh0)
At this case ,It's not the + operator overloading you're looking for,I think you need to overload the () ,revise @ne555 for the zero,one parameter problem

for me I'll still with this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# include <iostream>
using namespace std;

class com
{
    double x,y;
    public:
        void getdata(double,double);
        void display(void);// This function does not necessirly need that argument
        com operator ()(com c,com d);
        com operator =(com sum);
};


void com::getdata (double a,double b)
{
    x=a;
    y=b;
}

void com::display(void)
{
    cout << x << "+i" << y << endl;
}

com com::operator ()(com c,com d)
{
    com finl;

    finl.x= x + c.x+d.x;
    finl.y= y + c.y+d.y;

    return(finl);
}

com com::operator =(com sum)
{
    x = sum.x;
    y = sum.y;

    return(*this);
}

int main()
{
    com c1,c2,c3;
    c1.getdata(2.3,3.4);
    c2.getdata(5.2,4.4);
    c3.getdata(0.0,0.0);
    c3 = c3(c1,c2);
    c3.display();

    return 0;
}


I hope that helps
Topic archived. No new replies allowed.