Need to call function in another class

Hello,
I really appreciate your help I have just started with c++ programming so I have some problems, the problem is that I do not know how to call my function in the main
I have Class Patient that contains this method :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 void Patient:: fillPatient(vector <Patient>&  NewPatient)
 {     int etat =0;
     double LatPa, LongPa;
     double min = 0.5;
     double max = 15;
     //unsigned int size = NewPatient.size();
     for (unsigned int i =0; i<200; i++){
            LatPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            LongPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            etat ++;
            Patient NewPat(etat,LatPa, LongPa);
            NewPatient.push_back(NewPat);
    }

 } 


in the main I created my object and I don't know how to call the function :
1
2
3
4
5
 int main()
    {
     vector <Patient> myPat;
     Patient :: fillPatient(myPat);
      }



Thank you
Last edited on
You are trying to call your function statically; that is, without an object.
Either make it a free function (not inside of a class), or add static to the definition of the function.

Option #1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void fillPatient(vector <Patient>&  NewPatient)
{
     int etat =0;
     double LatPa, LongPa;
     double min = 0.5;
     double max = 15;
     //unsigned int size = NewPatient.size();
     for (unsigned int i =0; i<200; i++){
            LatPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            LongPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            etat ++;
            Patient NewPat(etat,LatPa, LongPa);
            NewPatient.push_back(NewPat);
    }

} 


Option #2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static void Patient:: fillPatient(vector <Patient>&  NewPatient)
{
     int etat =0;
     double LatPa, LongPa;
     double min = 0.5;
     double max = 15;
     //unsigned int size = NewPatient.size();
     for (unsigned int i =0; i<200; i++){
            LatPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            LongPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            etat ++;
            Patient NewPat(etat,LatPa, LongPa);
            NewPatient.push_back(NewPat);
    }

} 
Last edited on
the design may be a little off. I would make fillpatient fill a single one, not an array, and you might ahve another function that fills an array to use 1 of those each loop iteration.

that would look like

for(... sizeof vector)
mypat[i].fillpatient();

to do it the way you are doing it, you should make fillpatient NOT a member of the class as it does not manipulate the class, it manipulates a vector and isnt tied to the class. Your design of the class is tying the class itself to how it is used, in other words.
Last edited on
Hello,

Thank you a lot, it works now I added static only in Patient.h file and not in the Patient.cpp file

Patient.h
static void fillPatient(vector <Patient>&);

Patient.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Patient:: fillPatient(vector <Patient>&  NewPatient)
 {
     int etat =0;
     double LatPa, LongPa;
     double min = 0.5;
     double max = 15;
     //unsigned int size = NewPatient.size();
     for (unsigned int i =0; i<200; i++){
            LatPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            LongPa = min + ((max - min) * (rand () / (double) RAND_MAX));
            etat ++;
            Patient NewPat(etat,LatPa, LongPa);
            NewPatient.push_back(NewPat);
    }

 }


Thank you a lot
Registered users can post here. Sign in or register to post.