ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Calling another class object member function in a ROS cpp node

asked 2023-01-23 17:49:56 -0600

Dp gravatar image

Hi below is the code, how do i create object, initialize and call my B class object member function inside the rosnode point_callback function as below is my structure ?.

"in A.h header file"

class A{
public:
    A(){};
    virtual ~A(){};
    virtual float dosomething(int a) = 0;
};

"in B.h header file"

class B: public A{
public:
    B(){};
    virtual ~B(){};
    virtual float dosomething(int a ) override;
};

"In B.cpp header file"

"include B.h"
float B::dosomething(int a){
    //some code
    return b;
}

"in test_ros_node.cpp"

include "B.h"
class TestROSNode{

private:
        ros::Subscriber ik_sub;
public:
        IKnode(ros::NodeHandle *nh){
            ik_sub = nh->subscribe("/testpoint", 1000, &TestROSNode::point_callback, this);
        }
       void point_callback(const geometry_msgs::Point::ConstPtr& msg){
        //some code
        // I WANT TO ACCESS THE B CLASS dosomething(int a ) function here (how should i and where should i initialize the object and access the function here? )
        }
}

int main(){
    ros::init(argc, argv, "IKnode");
    ros::NodeHandle nh;
    TestROSNode ik_node = TestROSNode(&nh);
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-16 10:11:30 -0600

rreignier gravatar image

This is not a ROS question, but a C++ one. To be able to call an instance of the class B in your callback, the class TestROSNode needs to own an instance of the class B:

class TestROSNode{
private:
    ros::Subscriber ik_sub;
    B my_b;
    void point_callback(const geometry_msgs::Point::ConstPtr& msg){
       my_b.dosomething(msg.x):
    }
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2023-01-23 17:49:56 -0600

Seen: 116 times

Last updated: Feb 16 '23