cant access a private member using friend

i cant access (name)
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
class employee{
public:
    void setName(string,double);
    void print();
    employee();
    employee(string,double);
protected:
    double hourRate;
private:
    string name;
friend class partTimeEmp;
  friend istream& operator>>(istream& in,partTimeEmp& a);


};
 class partTimeEmp :public employee
 {
 public:
     void setInfo(string,double,double);
     void print();
     double calculatePay();
     partTimeEmp();
     partTimeEmp(string,double,double);
 private:
    double workHours;
  friend istream& operator>>(istream& in,partTimeEmp& a);

 };
 istream& operator>>(istream& in,partTimeEmp& a)
 {
     cout<<"enter name :"<<endl;
     in >> a.name;
     cout<<"enter hourly rate:"<<endl;
    in >> a.hourRate;
    cout<<"enter work hours:"<<endl;
    in >> a.workHours;


     return in;

the errors:
error: 'partTimeEmp' has not been declared
error: 'std::__cxx11::string employee::name' is private
error: within this context
The class itself doesn't need to be a friend here (at least, within your example), so you can remove line 11.

To fix this, add
class partTimeEmp;
before the definition of class employee { ... };
oh wow thank you so much
An alternative found here:
https://stackoverflow.com/questions/12522624/inheriting-friend-operators-in-c

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
72
73
74
#include <iostream>
#include <sstream>


class employee {
public:
    employee() = default;
    employee( std::string, double );

protected:
    double hour_rate {};

private:
    std::string name;

//friend:
    friend std::istream& operator>>( std::istream&, employee& );
};


employee::employee( std::string name_arg, double hour_rate_arg )
    : hour_rate { hour_rate_arg }
    , name { name_arg }
{
}


std::istream& operator>>( std::istream& in, employee& rhs )
{
    std::cout << "Enter name: ";
    in >> rhs.name;
    std::cout << "Enter hourly rate: ";
    in >> rhs.hour_rate;
    return in;
}


class partTimeEmp : public employee {
public:
    partTimeEmp() = default;
    partTimeEmp( std::string, double, double );

private:
    double work_hours {};

//friend:
    friend std::istream& operator>>( std::istream&, partTimeEmp& );
};


partTimeEmp::partTimeEmp( std::string name_arg,
                          double hour_rate_arg,
                          double work_hours_arg )
    : employee( name_arg, hour_rate_arg )
    , work_hours { work_hours_arg }
{
}


std::istream& operator>>( std::istream& in, partTimeEmp& rhs )
{
    in >> static_cast<employee &>( rhs );
    std::cout << "Enter work hours: ";
    in >> rhs.work_hours;
    return in;
}


int main()
{
    std::istringstream iss { "Ann 13 66.6" };
    partTimeEmp pte;
    iss >> pte;
}

Topic archived. No new replies allowed.