ros::init() must be called before creating a node handle
Hello,
I have defined a library (i.e. another ROS package) that node "X" depends upon. In the header file of the library I have a class that declares a node handle. When "X" is being preprocessed, the library's header file is pulled into "X".
When I run the system (via roslaunch), I immediately get this: "ros::init() must be called before creating a node handle" and the node crashes b/c it finds the node handle before it finds main(), which is where ros::init is called.
The question: How do I allow a node handle to exist in the library header file (so the library class can use it) without causing such an error?
Here is an example:
Lib.h:
class Z
{
public: ...
private: ros::NodeHandle nh;
};
Node "X" source (.cpp):
#include <Lib/Lib.h>
...
main()
{
Z z;
ros::init(...);
}
When I launch "X", which depends upon Lib (and by extension "Z"), the node handle is found before ros::init() is executed, even though the node handle is not used until afterwards...
Again, the runtime error is: "ros::init() must be called before creating a node handle"
Thanks a lot!