Need help with operator+ !!

int operator+ function returns an address i think, anybody knows why?
I need to add two objects and return int.


Here is the 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
#include <iostream>

using namespace std;

class T
{
    int one,two;

public:
    T(int one = 2, int two = 1)
    {
        cout << one << "/" << two << endl;
    }

    int operator+(T s)
    {
        int x = (one/two) + (s.one/s.two);
        cout << x;
        return x;
    }

};

int main()
{
    T one(7,5);
    T two;
    int i = one + two;
    return 0;
}
It does not return an address, it returns an int.

The problem is you never initialize the 'one' and 'two' members of your T class.

This is hard to see because you gave 3 different things the same name, so I'm going to rename your vars for this example. Note that the below code is 100% identical to yours, apart from the changed names:

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
#include <iostream>

using namespace std;

class T
{
    int one,two;

public:
    T(int a = 2, int b = 1)
    {
        // note that you are printing parameters 'a' and 'b' here, and not your
        //   member vars 'one' and 'two'.
        cout << a << "/" << b << endl;
        
        // also note that you do not initialize 'one' and 'two' to anything... so
        //  those vars are filled with random garbage.
    }

    int operator+(T s)
    {
        // note again, since 'one' and 'two' never were initialized, they have garbage.
        //   so here, 'x' is also going to be garbage
        int x = (one/two) + (s.one/s.two);
        cout << x;
        return x;
    }

};

int main()
{
    T foo(7,5);
    T bar;
    int i = foo + bar;
    return 0;
}
Last edited on
Oh okay Disch , now i see... so all i need to do is just :

1
2
this-> one = one;
this-> two = two;


in the default constructor ?
Last edited on
Do yourself a favour; don't name your parameters the same as the member variables.

1
2
3
4
5
6
public:
    T(int first_input_value, int second_input_value)
    {
       one  = first_input_value;
       two = second_input_value;
    }
Hi,

Also avoid naming your class T

T is often used as type parameter for template programming, so IMO it would be good to not see it being used like you have it.

Consider separating the class declaration and it's implementation in to separate files, .hpp and .cpp .

Consider using a member initialisation list instead of assignment in your constructor.

Don't have using namespace std; ,especially in header files. Just put std:: before each std thing, it's the best way, all the experts do that.

You can Google these things :+)
Oh thanks Moschops, i don't know why i name them the same always.

TheIdeasMan thanks a lot for helpful advices. I never understood why it is bad to use using namespace std; ? Is it like interrupting something or?
You can Google these things :+)
Topic archived. No new replies allowed.