private member error

I am getting the errors , add.miles, add.yards, add.feet, and add.inches in the '+' operator overload, but I do not understand why i am getting this error on this function and not on the '-' operator overload function. These are the two functions. any advice?

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
Distance operator+ (const Distance& d1, const Distance& d2)
{
    Distance add;
    
//errors here  
    add.miles = d1.miles + d2.miles; //miles is a private member of Distance
    add.yards = d1.yards + d2.yards; //yards is a private member of Distance
    add.feet = d1.feet + d2.feet; //feet is a private member of Distance
    add.inches = d1.inches + d2.inches; //inches is a private member of Distance
    
    Convert( add.inches, add);
    
    return add;
}

Distance operator- (const Distance& d1, const Distance& d2)
{
    Distance sub;
    
    sub.miles = d1.miles - d2.miles;
    sub.yards = d1.yards - d2.yards;
    sub.feet = d1.feet - d2.feet;
    sub.inches = d1.inches - d2.inches;
    
    if ( d1 > d2)
    {
        sub.SetDistance(DEFAULT_MILES, DEFAULT_YARDS, DEFAULT_FOOT, DEFAULT_INCHES);
    }
    return sub;
}
What does the Convert function look like? Is it not a member of the class and trying to modify private members?
This is the convert function
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
void Distance::Convert(int i, Distance& d)
{
    int footCount = DEFAULT_FOOT;
    int yardCount = DEFAULT_YARDS;
    int mileCount = DEFAULT_MILES;
    
    if ( i > INCHES_MILE)
    {
        mileCount = 1;
        while ( i - INCHES_MILE)
        {
            mileCount++;
            d.miles = mileCount;
        }
    }
    
    if( i > INCHES_YARD) //greater than 3 feet
    {
        yardCount= 1;
        while ( i < INCHES_MILE) //less than 1 mile
        {
            if ((i - INCHES_YARD) > 0)
            {
                yardCount++;
                d.yards = yardCount;
            }
        }
    if (i > DEFAULT_FOOT && i < INCHES_YARD) //0-36
    {
        if (i > INCHES_FOOT) // 12+
        {
            footCount = 1;
            while ( i < INCHES_YARD) //less than 36
                {
                    //foot = d.feet;
                    if ((i - INCHES_FOOT) > 0)
                    {
                        footCount++;
                        d.feet = footCount;
                    }
                }
        }
        d.inches = i;
    }
}
}
I guess I should also ask what error is the compiler throwing at you? It might be an issue of an infinite loop in the Convert function. Maybe you also need to modify your function call like such: add.Convert(0, add);, so the exact error would probably clear that up.
'miles' is a private member of Distance
'yards' is a private member of Distance
'feet' is a private member of Distance
'inches' is a private member of Distance

are the errors
Last edited on
Odd how one works and the other doesn't. The only thing I could point to is that you're using operator overloads that take two parameters which means it's defined outside the scope of the class. You probably have the operator- overload a friend of the class but not the operator+.
Topic archived. No new replies allowed.