struggling with friend class and array of objects

wanna use the array of objects and friend class concept here
but im getting too many errors couldnt resolve those please help
try to run the code you will get the errors i cant post all of them here

#include<iostream>
#include<string>
using namespace std;
class pis{
friend class boss;
public:
pis()
{ name="";
date_of_birth="";
blood_group="";
height=0;
weight=0;
insurance_number=0;
address="";
tele_num=0;
driv_lic=0;}
private:
string name;
string date_of_birth;
string blood_group;
float height;
int weight;
double insurance_number;
string address;
int tele_num;
double driv_lic;
};



class boss
{
public:
int num_rec;
void settotalrecords();
int gettotalrecords();
void takeuserinput(boss m[]);
void displayinfo(boss m[]);
};


void boss::settotalrecords()
{cout<<"enter the number of records to be inputted"<<endl;
cin>>num_rec;
}


int boss::gettotalrecords(){
return num_rec;
}

void boss::takeuserinput(boss m[])
{
for(int var=0;var<num_rec;var++)
{
cout<<"enter the name of the person"<<endl;
cin>>m[var].name;
cout<<"date of birth of the person"<<endl;
cin>>m[var].date_of_birth;
cout<<"enter blood group"<<endl;
cin>>m[var].blood_group;
cout<<"enter the height and weight of person"<<endl;
cin>>m[var].height;
cin>>m[var].weight;
cout<<"enter insurance number"<<endl;
cin>>m[var].insurance_number;
cout<<"enter adress"<<endl;
cin>>m[var].address;
cout<<"enter telphone number"<<endl;
cin>>m[var].tele_num;
cout<<"enter driving license number"<<endl;
cin>>m[var].driv_lic;
}
}

void boss::displayinfo(boss m[])
{
for(int var=0;var<num_rec;var++)
{
cout<<m[var].name<<"\n"<<m[var].date_of_birth<<"\n"<<m[var].blood_group<<"\n"<<m[var].height<<"\n";
cout<<m[var].weight<<"\n"<<m[var].insurance_number<<"\n"<<m[var].address<<"\n"<<m[var]<<"\n";
cout<<m[var].driv_lic<<"\n";
}
}



int main(){
cout<<"records program"<<endl;
boss temp;
temp.settotalrecords();
boss w[temp.gettotalrecords()];
int c;

do{
cout<<"enter 1 to make a record\n 2 for displaying records \n 3 for exit"<<endl;
cin>>c;
switch(c){
case 1:temp.takeuserinput(w);
break;
case 2:temp.displayinfo(w);
break;
default:cout<<"invalid choice man!"<<endl;

}
}
while(c!=3);
}
You have numerous problems.

1) You're misunderstanding how to use classes. You should not define a class and then pass an array of that class to a member function of that class. i.e. takeuserinput, displayinfo.
A member function of a class is implicitly passed a pointer (this) to the instance for which it was called.

2) In takeuserinput and displayinfo you're trying to access members of class pis. In order to do that, you need to access an instance of pis. You have no instance of pis. What I suspect you want is an array pis, not boss.

3) The following is not legal C++. In C++, an array size must be a constant known at compile time.
 
boss w[temp.gettotalrecords()];

Use a std::vector instead. You won;t need to know the maximum number of entries in advance.

4) If the user enters "3" to exit, he will get the message "invalid choice man!"

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<iostream>
#include<string>
#include <vector>
using namespace std;

class pis
{
public:
    pis()
    {   name="";
        date_of_birth="";
        blood_group="";
        height=0;
        weight=0;
        insurance_number=0;
        address="";
        tele_num=0;
        driv_lic=0;
    }
    void display()
    {   cout << name << "\n" << date_of_birth << "\n" << blood_group << "\n" << height << "\n";
        cout << weight << "\n" << insurance_number << "\n" << address << "\n" << tele_num << "\n";
        cout << driv_lic<<"\n";
    }
    void input ()
    {   cout<<"enter the name of the person"<<endl;
        cin >> name;
        cout<<"date of birth of the person"<<endl;
        cin >> date_of_birth;
        cout<<"enter blood group"<<endl;
        cin >> blood_group;
        cout<<"enter the height and weight of person"<<endl;
        cin >> height;
        cin >> weight;
        cout<<"enter insurance number"<<endl;
        cin >> insurance_number;
        cout<<"enter adress"<<endl;
        cin >> address;
        cout << "enter telphone number"<<endl;
        cin >> tele_num;
        cout<<"enter driving license number"<<endl;
        cin >> driv_lic;
    }
private:
    string name;
    string date_of_birth;
    string blood_group;
    float height;
    int weight;
    double insurance_number;
    string address;
    int tele_num;
    double driv_lic;
};

class boss
{   vector<pis>     m_pis;

public:
    int num_rec;
    void settotalrecords();
    int gettotalrecords();
    void takeuserinput();
    void displayinfo();
};

void boss::settotalrecords()
{   cout<<"enter the number of records to be inputted"<<endl;
    cin>>num_rec;
}

int boss::gettotalrecords()
{   return num_rec;
}

void boss::takeuserinput()
{   pis     temp;
    for(int var=0;var<num_rec;var++)
    {   temp.input ();
        m_pis.push_back (temp);
    }
}

void boss::displayinfo()
{   for(size_t var=0; var<m_pis.size(); var++)
        m_pis[var].display ();
}

int main()
{   cout<<"records program"<<endl;
    boss temp;
    temp.settotalrecords();
    int c;

    do
    {   cout<<"enter 1 to make a record\n 2 for displaying records \n 3 for exit"<<endl;
        cin>>c;
        switch(c)
        {   case 1: temp.takeuserinput();
                break;
            case 2: temp.displayinfo();
                break;
        default:    cout<<"invalid choice man!"<<endl;
        }
    } while(c!=3);
}


Note that input and display have been added to class pis where they belong.
Also friendship is no longer required since the member variables are only being accesses from functions that are members of the class.
thanks for your advice i appreciate
but i didnt get the term "instance of class" what is it?how to use it?
also tell how can i access private members of the class singly.. dont wanna consider array of objects
i didnt get the term "instance of class"

Line 91 of my code makes an instance of class boss called temp. temp is the instance.

also tell how can i access private members of the class singly

Try to understand the code I posted. Pay particular attention to pis::display and pis::input.
Both of these functions deal with a single instance of pis.

got it!
Topic archived. No new replies allowed.