Reputation: 23
i'm writing this code in omnet++ that should send messages each T time from different nodes to one central node. My problem now is that the handleMessage function doesnt see the classes and function i created, while inizialize sees everything; im fairly new to programming and a newbie to the c++ language :). CODE:
using namespace omnetpp;
class Net : public cSimpleModule
{
public:
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
class Central_Node : public Net
{
simtime_t time_stamps[];
int index;
public:
void addTimeStamp()
{
int array_size = sizeof(time_stamps);
time_stamps[array_size] = SimTime();
}
void deleteSignal(cMessage *msg)
{
delete msg;
}
};
class Sensor : public Net
{
int index;
double delay;
public:
Sensor(int a, double b)
{
index = a;
delay = b;
}
void sendMessage()
{
cMessage *msg = new cMessage("Signal");
send(msg, 0);
cMessage *SelfMsg = new cMessage("SelfMessage");
scheduleAt(simTime() + delay, SelfMsg);
}
};
Define_Module(Net);
void Net::initialize()
{
Central_Node centralNode;
Sensor Sensor1 (1,100);
Sensor Sensor2 (2,210);
Sensor1.sendMessage();
Sensor2.sendMessage();
}
void Net::handleMessage(cMessage *msg)
{
if (getIndex() == 0)
{
simtime_t ArrivalTime = centralNode.addTimeStamp();
centralNode.deleteSignal(*msg);
}
if (getIndex() == 1)
{
Sensor1.sendMessage();
}
if (getIndex() == 2)
{
Sensor2.sendMessage();
}
}
The error I get is: use of undeclared identifier 'centralNode'
on the line simtime_t ArrivalTime = centralNode.addTimeStamp();
.
Upvotes: 2
Views: 169
Reputation: 4673
The problem is that at the point of use, centralNode
is not a defined variable:
void Net::handleMessage(cMessage *msg)
{
if (getIndex() == 0)
{
// Error: use of undeclared identifier 'centralNode'
simtime_t ArrivalTime = centralNode.addTimeStamp();
centralNode.deleteSignal(*msg);
}
[...]
}
You create a centralNode
variable in the Net::initialize()
member function, but that is a local variable within the function, not a class member. That centralNode
variable in the Net::initialize()
function is naturally not visible within any other member functions, including the Net::handleMessage
member function.
There are two possible solutions.
You can declare a local variable in Net::handleMessage()
, just like the Net::initialize()
function does. This Central_Node
object will "disappear" (be destroyed) once it goes out of scope.
void Net::handleMessage(cMessage *msg)
{
if (getIndex() == 0)
{
Central_Node centralNode; // Instantiate here before use.
simtime_t ArrivalTime = centralNode.addTimeStamp();
centralNode.deleteSignal(*msg);
}
[...]
}
You can declare a class data member in the Net
class. This is complicated, however, because the Net
class would contain a Central_Node
data member, but the CentralNode
class itself inherits from Net
.
To break this circular dependency, you can forward-declare Central_Node
and use a pointer within the Net
class:
class Central_Node; // Forward declaration
class Net : public cSimpleModule
{
// unique_ptr required to break circular dependency
std::unique_ptr<Central_Node> centralNode;
[...]
};
Often, when code exhibits a circular dependency, it is a sign that the abstractions or relationships expressed in code are not the right ones.
In this case, it seems wrong that a Central_Node
inherits from a Net
(presumably "Network") class. Inheritance is classically used to express an "is-a" relationship, and it is very odd to say "A Central_Node (or any Node) is a Network". Instead, a Node (including a CentralNode) is a part of a Network, or alternatively, a Network contains Nodes.
Separately from all this, I think there is a bug in the Central_Node:: addTimeStamp()
member function, since the array data member is never initialized. I suspect that, as written, this will cause a segfault or other crash at runtime.
Upvotes: 1