This is doing my head in...
SHORT VERSION:
I want to be able to save the address of a vector in another class.
LONG VERSION:
I am moddeling a swarm of robots in a simulator. I have 2 classes- Robot and Controller. In my simulator I have a vector of each (e.g. 20 robots with 20 seperate controllers). Within the Robot class there are some vectors that store some state information, such as the UIDs of the Neighbour robots, and also distances etc. Just think of these as vectors of ints and doubles etc. These vectors are updated every step of the simulation.
I want the Controller class to be able to point to the addresses of these Neighbour UIDs and distance vectors etc. i.e. within the Controller class I want a vector of ints that point to the address of the Robot vector. Thereby as the Robot vectors are updated the Controller vectors are automatically updated without me having to send and copy the vector each step.
PROBLEM:
The addresses do not seem to be saved properly.
CODE:
// Main code
During the Init function I find that:
AA11 = BB11 = BB22 but when the code returns to the init function the address has changed and the address has changed and AA22 does not equal AA11 etc..
Whjat am i doing worng. What is the best way to achieve what I want. I hope you understand my goal, nothing oo difficult. I just want a pointer to the Robot vector instead of having to allocate more memory and create a new vector in Controller each and every time step.
SHORT VERSION:
I want to be able to save the address of a vector in another class.
LONG VERSION:
I am moddeling a swarm of robots in a simulator. I have 2 classes- Robot and Controller. In my simulator I have a vector of each (e.g. 20 robots with 20 seperate controllers). Within the Robot class there are some vectors that store some state information, such as the UIDs of the Neighbour robots, and also distances etc. Just think of these as vectors of ints and doubles etc. These vectors are updated every step of the simulation.
I want the Controller class to be able to point to the addresses of these Neighbour UIDs and distance vectors etc. i.e. within the Controller class I want a vector of ints that point to the address of the Robot vector. Thereby as the Robot vectors are updated the Controller vectors are automatically updated without me having to send and copy the vector each step.
PROBLEM:
The addresses do not seem to be saved properly.
CODE:
Code:
class Controller {
std:: vector<int> * NeighbourUIDs_Vector;
void setNeighbours(std::vector<int> *NeighbourUIDs){
NeighbourUIDs_Vector = NeighbourUIDs
// DEBUG TEST:
printf("BB11 %i \n", &NeighbourUIDs); // These are equal BB11=BB22
printf("BB22 %i \n", &NeighbourUIDs_Vector);
};
void step(){
printf("ADDRESS1 %i \n", &NeighbourUIDs_Vector); // Wrong address
printf("DEBUG %i \n", NeighbourUIDs_Vector.size()); // seg faults
}
};
Code:
class robot{
std:: vector<int> NeighbourUIDs_Vector;
// some other stuff
};
// Main code
Code:
Robot robot; // this is actually also a vector of e.g.20 robots
Controller controller; // this is actually also a vector of e.g.20 robots
findNeighbours(){
// find neighbour robots, blah blah. This code works fine
robot.NeighbourUIDs_Vector.clear(); // clear vector
robot.NeighbourUIDs_Vector = ..... // e.g. I update the vector
}
void Init(){
robot = new Robot(); //this is basically repeated for all robots in the robot vector
controller = new Controller(); // same here
findNeighbours(); // Assign the neighbourUIDs to each robot
// DEBUG
printf("AA11 %i \n", &robot.NeighbourUIDs_Vector);
controller.setNeighbours(&robot.NeighbourUIDs_Vector); // pass the address
// DEBUG
printf("AA22 %i \n", &controller.NeighbourUIDs_Vector);
controller.step(); // will now seg Fault.
}
During the Init function I find that:
AA11 = BB11 = BB22 but when the code returns to the init function the address has changed and the address has changed and AA22 does not equal AA11 etc..
Whjat am i doing worng. What is the best way to achieve what I want. I hope you understand my goal, nothing oo difficult. I just want a pointer to the Robot vector instead of having to allocate more memory and create a new vector in Controller each and every time step.