Trouble using friend of classes

I'm getting the error that Person's variables are private in the overloaded operator<< of the package class I will bold the variables and where im trying to use them.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Person
{
    private:
    string Name,
    Address,
    State,
    City,
    Zip;

    public:
    friend istream &operator>> (istream &, Person &);
    friend class Package;
    friend class TwoDayPackage;
    friend class OvernightPackage;

};

class Package
{
    protected:
    Person Sender;
    Person Reciever;
    double CostPerOz;
    int WeightOz;
    static int NumberProcessed;

    public:
    friend ostream &operator<< (ostream &, Package);
    friend istream &operator>> (istream &, Package &);
    int getProcessed(){return NumberProcessed;}
    void addProcessed(){NumberProcessed++;}
};

/**********************************************
*             Start of Package code.h           *
*                                             *
*                                             *
*                                             *
**********************************************/
ostream &operator<< (ostream &oParam, Package pParam)
{
    oParam << left << "Package #" << pParam.NumberProcessed << endl;
    oParam << left << "\nSender:" << endl;
    oParam << right << setw(20) << "Name: " << left << pParam.Sender.Name << endl;
    oParam << right << setw(20) << "Address: " << left << pParam.Sender.Address << endl;
    oParam << right << setw(20) << "City, State, Zip: " << left << pParam.Sender.City
                                                        << ", " << pParam.Sender.State
                                                        << "  " << pParam.Sender.Zip << endl;
    oParam << "\n" << endl;
    oParam << left << "Reciver:" << endl;
    oParam << right << setw(20) << "Name: " << left << pParam.Reciever.Name << endl;
    oParam << right << setw(20) << "Address: " << left << pParam.Reciever.Address << endl;
    oParam << right << setw(20) << "City, State, Zip: " << left << pParam.Reciever.City
                                                        << ", " << pParam.Reciever.State
                                                        << "  " << pParam.Reciever.Zip << endl;
    oParam << "Shipping Cost for " << pParam.WeightOz << " oz. @$" << fixed << setprecision(2) << pParam.CostPerOz << "/ounce is " << (pParam.CostPerOz * (double)pParam.WeightOz) << endl;
    oParam << "\n\n" << endl;
    return oParam;
}

istream &operator>> (istream &iParam, Package &pParam)
{
    iParam >> pParam.WeightOz;
    iParam.ignore(10, '\n');
    iParam >> pParam.CostPerOz;
    iParam.ignore(10, '\n');
    iParam >> pParam.Sender;
    iParam >> pParam.Reciever;
    return iParam;

}
Last edited on
I just overloaded the operator<< for person class, and used that in my overload for Package class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ostream &operator<< (ostream &oParam, Package pParam)
{
    oParam << left << "Package #" << pParam.NumberProcessed << endl;
    oParam << left << "\nSender:" << endl;
    oParam << right << setw(20) << "Name: " << left << pParam.Sender.Name << endl;
    oParam << right << setw(20) << "Address: " << left << pParam.Sender.Address << endl;
    oParam << right << setw(20) << "City, State, Zip: " << left << pParam.Sender.City
                                                        << ", " << pParam.Sender.State
                                                        << "  " << pParam.Sender.Zip << endl;
    oParam << "\n" << endl;
    oParam << left << "Reciver:" << endl;
    oParam << right << setw(20) << "Name: " << left << pParam.Reciever.Name << endl;
    oParam << right << setw(20) << "Address: " << left << pParam.Reciever.Address << endl;
    oParam << right << setw(20) << "City, State, Zip: " << left << pParam.Reciever.City
                                                        << ", " << pParam.Reciever.State
                                                        << "  " << pParam.Reciever.Zip << endl;
    oParam << "Shipping Cost for " << pParam.WeightOz << " oz. @$" << fixed << setprecision(2) << pParam.CostPerOz << "/ounce is " << (pParam.CostPerOz * (double)pParam.WeightOz) << endl;
    oParam << "\n\n" << endl;
    return oParam;
}


would just become
1
2
3
4
5
6
7
8
9
10
11
ostream &operator<< (ostream &oParam, Package pParam)
{
    oParam << left << "Package #" << pParam.NumberProcessed << endl;
    oParam << left << "\nSender:" << endl;
    oParam << Sender;
    oParam << "Reciever: " << endl;
    oParam << Reciever;
    oParam << "Shipping Cost for " << pParam.WeightOz << " oz. @$" << fixed << setprecision(2) << pParam.CostPerOz << "/ounce is " << (pParam.CostPerOz * (double)pParam.WeightOz) << endl;
    oParam << "\n\n" << endl;
    return oParam;
}
I'm getting the error that Person's variables are private in the overloaded operator<< of the package class


Indeed they are. And you can use the same method to handle this as you did for operator>>. Make it a friend of the class.
Last edited on
FWIW, there is no point in making member variables private if you're just going to make every class a friend.

You might as well just make them public. They're not encapsulated either way.
i tryed making it a friend look here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person
{
    private:
    string Name,
    Address,
    State,
    City,
    Zip;

    public:
    friend istream &operator>> (istream &, Person &);
    friend class Package;
    friend class TwoDayPackage;
    friend class OvernightPackage;

};

i made class package a friend
Why would Package need to be a friend for operator<< to have access to Person's privates?

operator<< is not a member of Package.

Make operator<< a friend. Or, as Disch says, just make the members public.
Topic archived. No new replies allowed.