我将尝试创建一个类,只能被实例化一次,并且可以向其中添加ROS主题订阅,其中回调是相同成员函数的模板特化。
ros_topic_subscriber.h文件中的类如下:
在我的情况下,topicCallback 的一个可能的模板特化是(在 ros_topic_subscriber.cpp 文件中)
现在,我遇到的编译器错误是这个:
这种情况为什么会出现错误信息? 是否可以实现这种类? 如何修复此错误?
ros_topic_subscriber.h文件中的类如下:
class ROSTopicSubscriber
{
public:
ROSTopicSubscriber() {}
~ROSTopicSubscriber() {}
template<typename ROSMessageType>
int init
(
const ros::NodeHandle &controller_nh,
const std::string& topic_name,
unsigned int buffer_size
)
{
ros::Subscriber sub = controller_nh.subscribe(
topic_name,
buffer_size,
&ROSTopicSubscriber::topicCallback<ROSMessageType>,
this
);
}
/*! \brief Implement this function for your own message!
*/
template<typename ROSMessageType>
void topicCallback(const typename ROSMessageType::ConstPtr& msg);
private:
// No copying of this class is allowed !
ROSTopicSubscriber(const ROSTopicSubscriber& other) = delete;
ROSTopicSubscriber(ROSTopicSubscriber&& other) = delete;
ROSTopicSubscriber& operator=(const ROSTopicSubscriber& other) = delete;
ROSTopicSubscriber& operator=(ROSTopicSubscriber&& other) noexcept = delete;
};
在我的情况下,topicCallback 的一个可能的模板特化是(在 ros_topic_subscriber.cpp 文件中)
template<>
void ROSTopicSubscriber::topicCallback<geometry_msgs::Pose>
(
const geometry_msgs::Pose::ConstPtr& msg
)
{
std::cout << "msg\n";
}
为了使用这个类,可以做如下操作:
topic_subscriber_.init<geometry_msgs::Pose>
(n, "/wintracker/pose", 100);
现在,我遇到的编译器错误是这个:
ros_topic_subscriber.h:66:3:
error: passing ‘const ros::NodeHandle’ as ‘this’ argument of
‘ros::Subscriber ros::NodeHandle::subscribe(
const string&,
uint32_t,
void (T::*)(const boost::shared_ptr<const M>&),
T*,
const ros::TransportHints&)
[with M = geometry_msgs::Pose_<std::allocator<void> >;
T = hiqp::ROSTopicSubscriber;
std::string = std::basic_string<char>;
uint32_t = unsigned int]’
discards qualifiers [-fpermissive]
这种情况为什么会出现错误信息? 是否可以实现这种类? 如何修复此错误?