ROS C++ Publisher and Subscriber in the same node error

Hello,
How do I create a single node that has both a subscriber AND a publisher?
I need it to solve the quiz at the end of the forth model.
When I try to write the following
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
ros::Subscriber sub = nh.advertise<sensor_msgs::LaserScan>("LaserScan", 1000);

I get the following error:
"No viable conversion from ‘ros::Publisher’ to ‘ros::Subscriber’ "

I’ve looked for a solution for a few days but I can’t figure how to do it.

Hello @lneharde,

The subscriber definition is wrong. It has to be like this:

ros::Subscriber sub = nh.subscribe("/topic_name", 1000, callback_function);

Remember that subscribers have always a related callback function, which will be triggered when a new message is published into the topic.

Best,