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 (unsignedint 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);
}
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 (unsignedint 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
staticvoid Patient:: fillPatient(vector <Patient>& NewPatient)
{
int etat =0;
double LatPa, LongPa;
double min = 0.5;
double max = 15;
//unsigned int size = NewPatient.size();
for (unsignedint 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);
}
}
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.