I wrote 2 classes, Agent and Timing. The third class will contain the main() function and manage it all. The goal is to create n instances of Agent and a SINGLE instance of Timing. It's important to mention that Agent uses Timing fields and Timing uses Agent functions.
How can I turn Timing to singleton?
//Agent.h
#ifndef Timing_h
#define Timing_h
#include <string>
#include "Timing.h"
class Agent{
public:
Agent(std::string agentName);
void SetNextAgent(Agent* nextAgent);
Agent* GetNextAgent();
void SendMessage();
void RecieveMessage(double val);
// static Timing runTime;
What I thought would solve my problem but I got:
'Timing' does not name a type
~Agent();
private:
std::string _agentName;
double _pID;
double _mID;
Agent* _nextAgent;
};
#endif
//Timing.h
#ifndef Timing_h
#define Timing_h
class Timing{
private:
typedef struct Message{
Agent* _agent;
double _id;
}Message;
typedef Message* MessageP;
Message** _messageArr;
static int _messagesCount;
public:
Timing();
void AddMessage(Agent* agent, double id);
void LeaderElected(string name);
void RunTillWinnerElected();
~Timing();
};
#endif
Is this really the way to create a singleton and if it is what is the problem? And if not, how can I turn it to a singleton?
This bit looks suspicious in Agent.h ...
#ifndef Timing_h
#define Timing_h
Seems like it's the same define-guard in Timing.h. Therefore Timing will not be included in Agent.
Change it to
#ifndef Agent_h
#define Agent_h
Nope, the wikipedia page on the singleton pattern has a good example as well as a nice write up of how a singleton is actually constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With