gpt4 book ai didi

c++ - SFINAE 模板错误

转载 作者:行者123 更新时间:2023-11-28 05:50:18 25 4
gpt4 key购买 nike

我正在使用 SFINEA 订阅 ROS 中的通用主题,它会监听每个主题,并使用 SFINEA 返回 header.stamp 时间(如果存在)。这可以更快地反序列化。唯一的问题是我在设置订户时遇到问题。我不断收到以下编译错误:

CMakeFiles/performance_tracker.dir/src/performance_tracker.cpp.o: 
In function PerformanceTracker::topicCallback(boost::shared_ptr<topic_tools::ShapeShifter>)
ros/src/performance_tracker/src/performance_tracker.cpp:32:
undefined reference to boost::disable_if<timewarp::has_header<boost::shared_ptr<topic_tools::ShapeShifter> >, ros::Time>::type
timewarp::extractTime<boost::shared_ptr<topic_tools::ShapeShifter> >(boost::shared_ptr<topic_tools::ShapeShifter>)

主要

 // Subscribe To Generic Message 
_sub = _nh.subscribe( _topicName, 1, &PerformanceTracker::topicCallback, this);


void PerformanceTracker::topicCallback(const boost::shared_ptr<topic_tools::ShapeShifter> data){
//Current Time
ros::Time begin = ros::Time::now();
ros::Time timePublished = timewarp::extractTime<boost::shared_ptr<topic_tools::ShapeShifter>>(data);
}

命名空间类

namespace timewarp
{
template <typename T>
struct has_header {
typedef char yes[1];
typedef char no[2];

template <typename C>
static yes& test(typename C::_header_type*);

template <typename>
static no& test(...);

// If the "sizeof" the result of calling test<T>(0) would be equal to the sizeof(yes),
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};

template<class MsgType>
typename boost::enable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<topic_tools::ShapeShifter> data)
{
boost::shared_ptr<MsgType> ptr = data->instantiate<MsgType>();
assert(ptr);

return ptr->header.stamp;
}

template<class MsgType>
typename boost::disable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<topic_tools::ShapeShifter>);

}

最佳答案

您正在传递 boost::shared_ptr<topic_tools::ShapeShifter>类型为 MsgType 到 has_header<T>而不是 topic_tools::ShapeShifter .

我相信你想做的事:

ros::Time timePublished = timewarp::extractTime<topic_tools::ShapeShifter>(data);

并在使用时使用 const refs 参数 :)

template<class MsgType>
typename boost::enable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<MsgType>& data)
{
boost::shared_ptr<MsgType> ptr = data->instantiate<MsgType>();
assert(ptr);

return ptr->header.stamp;
}

template<class MsgType>
typename boost::disable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<MsgType>&)
{
return ros::Time::now();
// or whatever ros time you need to return for types without headers
}
}

关于c++ - SFINAE 模板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com