I have a three way cyclic dependency between three header files, each of which has one class within it. This is the relevant portion of the Agent2 class:
#ifndef BUYINGJEANS1_H
#define BUYINGJEANS1_H
class Agent2{
public:
Agent2(){}
double getAdvertisingFash(int currentTime, int sFash, int brand){
Agent1 agent1;
Agent3 agent3;
double probabilityOfChangingFashion = (exp((-advertisementArray[currentTime+1][brand]*pow(abs(min(fashionArray[brand]+1,4.0)-sFash),D))+(-alphaArray[2][2]*pow(abs(fashionArray[brand]-sFash),AArray[2][2]))) * (exp((-(agent1.alphaArray[2][1])*pow(abs((agent1.fashionArray[brand])-sFash),AArray[2][1]))))*(exp((-(agent3.alphaArray[2][3])*pow(abs((agent3.fashionArray[brand])-sFash),AArray[2][3])))))/getZScoreAdvertisingFash(currentTime,brand);
return probabilityOfChangingFashion;
}
#endif
An equivalent function exists in the Agent1 and Agent3 classes, except Agent1 would use Agent2 and Agent3, and Agent3 would use Agent1 and Agent2.
I know that forward declaration won’t work here because I obviously need to use the members of classes Agent2 and Agent3. When I run a main function in a separate cpp file that uses this code, I receive an undeclared identifier error for Agent1 and Agent3.
Anyone know how I can resolve this in the simplest manner possible?
You will need to move the body of one or more of your functions into a
.cppfile instead of the header. This way,Agent1andAgent3can include theAgent2header without theAgent2header needing them.You only need to move the implementation of the functions of one of these classes to break the cyclic dependency, but you may want to do it for all three to be consistent and avoid similar problems in the future.
In Agent2.H:
And in Agent2.cpp: