Comunication between classes

Hi everyone. I have 3 files on my proyect, Doctor.h, Appointment.h and main.cpp.

The main.cpp includes a menu and calls Doctor.h and Appointment.h methods. Both classes needs to capture information, and Appointment cant continue if a doctor doesnt exist first, so... i need to send a variable from a class to another to validate this.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Doctor.h"
#include "Appointment.h"

int main{
   Doctor doc;
   Appointment app;

   doc.capture();

   app.capture();
}


Doctor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Doctor{
    public:
        int pos;
    public:
        void capture();
        int retPos();
};

void Doctor::capture(){
  //a lot of things
   pos++
}

int retPos(){
   cout << Doctor.pos; //This is useless, just to see the value of -pos-
   return Doctor::pos;


Appointment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Doctor.h"
class Appointment{
    public:
        int pos;
        Doctor Doc;
    public:
        void capture();
};

void Doctor::capture(){
   //I need here the pos variable from Doctor.h, im trying like this:
 
  Doc.retPos(); 
   //It calls the function, but the pos value is always 0, even after use    //doc.capture(); in the main.cpp
}


if i call the same function ("doc.retPos()") in main.cpp , here the value of pos is showed, everytime i use doc.capture(); the value increase in 1. A guy tell me something about friend classes, i tried something like friend int retPos();... but i dunno how to implement it... it just gives me a bunch of errors.

Thanks in advance, i hope somebody could help me.
You will actually want to have two variables for this job, one being the static instance of "pos" that is shared between instances of the class and the other being the variable that is unique to each instance. Also, regarding Lines 10 - 15, what are you trying to do here?
Oh man, is kinda hard explaining cuz' English is not my first language...

Well, I just need a simple validation, if(a!=b)...

I mean

if (int somethingInputInAppointment != pos)...

The thing is that pos is in Doctor.h, and I don't know how to communicate to Appointment.h

If I try the same validation in main.cpp it works, with something like

if(app.something != doc.pos)

From "Main()", if you pass doc.pos to whatever function you are testing with in your "Appointment" then your just passing an integer. That should be simple enough to do.
but doc.pos doesn't have to pass through main. It has to pass directly from Doctor to Appointment. , with something like this, the value of Doc.pos is always 0
Appointment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
 
#include "Doctor.h"
class Appointment{
    public:
        
        Doctor Doc;
    public:
        void capture();
};

void Doctor::capture(){
   cout >> Doc.pos;
}



Last edited on
Execution starts at "Main()", the variable has to be passed from somewhere and with what you have written "Main()" was the best bet. You can pass it from a function called "CandyLand()" for all the difference it will make but code in header files is not executed until it is called and variables outside of their scope cannot be called.
OK, I think I got it... I'll try as soon as I get home... The my variable pos, in order to reach Appointment.h, has to pass first trough main using a function.

Doctor.h -> main.cpp —> Appointment.h

Am I right?
Almost, the variable "pos" only exists in the scope of the instance of the class that it is part of. If the instance of the "Doctor" class exists in "Main()" then that is where the variable should be passed from.
OK, one more thing. This variable, is gonna be an array, how do I pass the whole array through functions? =S
Topic archived. No new replies allowed.