引用属于 class 的函数
Refer to a function that is member of a class
所以我有这个意大利面条式代码项目,我正在尝试使其面向对象。不幸的是,我遇到了我不太理解的错误,所以我尝试创建极简代码,它会抛出相同的错误,而不会抛出相同的错误。
所以这是一些编译的简约代码:
(文件名为 "ims.cpp")
#include <cstdio>
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/InteractiveMarker.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <ros/param.h>
#include <fstream>
#include <cmath>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
void doNothing(const InteractiveMarkerFeedbackConstPtr &feedback){
}
void testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &doNothing);
}
int main(){}
说明:这是一个 ROS(机器人操作系统)项目,但我仍然认为这是一个一般的 c++ 问题,所以我没有在他们的 "ros::answers" 论坛上提问。请不要对类型感到困惑,我们会解决问题的。
函数 "interactive_markers::InteractiveMarkerServer.insert" 需要一个 "visualization_msgs::InteractiveMarker &" 和一个具有 "InteractiveMarkerFeedbackConstPtr &" 类型参数的函数,如所提供的。
见:http://docs.ros.org/jade/api/interactive_markers
/html/classinteractive__markers_1_1InteractiveMarkerServer.html
所以抛出我的错误的最小代码 将是 "doNothing" 函数中根本没有所需参数的代码,如下所示:
(文件名为 "ims.cpp")
#include <as above>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
void doNothing(){
}
void testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &doNothing);
}
int main(){}
抛出错误:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
from /usr/include/boost/function.hpp:64,
from /opt/ros/indigo/include/ros/forwards.h:40,
from /opt/ros/indigo/include/ros/common.h:37,
from /opt/ros/indigo/include/ros/ros.h:43,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::void_function_invoker1<FunctionPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionPtr = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’:
/usr/include/boost/function/function_template.hpp:934:38: required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’
/usr/include/boost/function/function_template.hpp:722:7: required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1069:16: required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:40:42: required from here
/usr/include/boost/function/function_template.hpp:112:11: error: too many arguments to function
BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));
这有道理,对吧?该函数没有所需的参数,因此编译器会报错。
所以现在我们到了真正的问题: 在面向对象的代码中,我有同样的问题:
头文件
(文件名为 "ims.h")
#include <cstdio>
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/InteractiveMarker.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <ros/param.h>
#include <fstream>
#include <cmath>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
class IMS{
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
IMS();
void doNothing(const InteractiveMarkerFeedbackConstPtr &feedback);
void testServer();
int main();
};
菲律宾共产党声明
(文件名为 "ims.cpp")
#include <ims.h>
IMS::IMS(){}
void IMS::doNothing(const InteractiveMarkerFeedbackConstPtr &feedback){
}
void IMS::testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &IMS::doNothing); //other options that didn't work either: doNothing);//*this->doNothing(const InteractiveMarkerFeedbackConstPtr &feedback));//*this->doNothing());//this->doNothing);//&this->doNothing);//&IMS::doNothing);//&doNothing);
}
int IMS::main(){}
抛出错误:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
from /usr/include/boost/function.hpp:64,
from /opt/ros/indigo/include/ros/forwards.h:40,
from /opt/ros/indigo/include/ros/common.h:37,
from /opt/ros/indigo/include/ros/ros.h:43,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/include/ims.h:3,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:1:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::function_void_mem_invoker1<MemberPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with MemberPtr = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’:
/usr/include/boost/function/function_template.hpp:934:38: required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’
/usr/include/boost/function/function_template.hpp:722:7: required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1069:16: required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:19:47: required from here
/usr/include/boost/function/function_template.hpp:225:11: error: no match for call to ‘(boost::_mfi::mf1<void, IMS, const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&>) (const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&)’
BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
诊断:
我的一个朋友实际上已经诊断出问题的可能来源:显然编译器将代码重新格式化为这个
void IMS::doNothing(IMS this, const InteractiveMarkerFeedbackConstPtr &feedback){
这当然会导致错误,因为参数不再符合我们的预期。
所以这是我的要求:
这个诊断是否正确?
如果是:可以解决吗?如何解决?
else: 实际问题是什么?
感谢所有完整阅读这篇长文章的人,并提前感谢您的回答!
解决方案(根据"einpoklum"的建议)
最符合我的想法的解决方案是 lambda,创建一个函数来包装恶意 "this" 参数。
std::function nothing =
[this] (const InteractiveMarkerFeedbackConstPtr &feedback) {this->doNothing(feedback);};
server->insert(inter_marker, nothing);
非常感谢您和所有费心阅读整个主题的人,对于我难以理解地提出问题感到抱歉。
试图穿过堆积如山的文字并冒险回答:我认为(但不确定)问题是你试图通过一个(非静态)成员函数,就好像它是一个独立的函数一样。
那应该行不通,因为您不能调用成员函数 "just like that" - 它必须有一个关联的对象。在实现层面上,需要用 class 实例的地址调用该段代码,以作为 this
对象。
在这些情况下您可以做的是:
- 使用
std::mem_fn
包装您的成员函数以获得适当的函数,额外的第一个参数是实例,或者
- 使用 lambda 以某种方式实例化对象(或仅将实例作为引用)并调用该实例的方法。 lambda 本身将退化为一个独立函数,您可以传递其指针。
- 创建方法
static
- 如果它实际上不使用任何特定于实例的数据。
所以我有这个意大利面条式代码项目,我正在尝试使其面向对象。不幸的是,我遇到了我不太理解的错误,所以我尝试创建极简代码,它会抛出相同的错误,而不会抛出相同的错误。
所以这是一些编译的简约代码:
(文件名为 "ims.cpp")
#include <cstdio>
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/InteractiveMarker.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <ros/param.h>
#include <fstream>
#include <cmath>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
void doNothing(const InteractiveMarkerFeedbackConstPtr &feedback){
}
void testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &doNothing);
}
int main(){}
说明:这是一个 ROS(机器人操作系统)项目,但我仍然认为这是一个一般的 c++ 问题,所以我没有在他们的 "ros::answers" 论坛上提问。请不要对类型感到困惑,我们会解决问题的。
函数 "interactive_markers::InteractiveMarkerServer.insert" 需要一个 "visualization_msgs::InteractiveMarker &" 和一个具有 "InteractiveMarkerFeedbackConstPtr &" 类型参数的函数,如所提供的。 见:http://docs.ros.org/jade/api/interactive_markers /html/classinteractive__markers_1_1InteractiveMarkerServer.html
所以抛出我的错误的最小代码 将是 "doNothing" 函数中根本没有所需参数的代码,如下所示:
(文件名为 "ims.cpp")
#include <as above>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
void doNothing(){
}
void testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &doNothing);
}
int main(){}
抛出错误:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
from /usr/include/boost/function.hpp:64,
from /opt/ros/indigo/include/ros/forwards.h:40,
from /opt/ros/indigo/include/ros/common.h:37,
from /opt/ros/indigo/include/ros/ros.h:43,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::void_function_invoker1<FunctionPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionPtr = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’:
/usr/include/boost/function/function_template.hpp:934:38: required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’
/usr/include/boost/function/function_template.hpp:722:7: required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1069:16: required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:40:42: required from here
/usr/include/boost/function/function_template.hpp:112:11: error: too many arguments to function
BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));
这有道理,对吧?该函数没有所需的参数,因此编译器会报错。
所以现在我们到了真正的问题: 在面向对象的代码中,我有同样的问题:
头文件
(文件名为 "ims.h")
#include <cstdio>
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/InteractiveMarker.h>
#include <interactive_markers/interactive_marker_server.h>
#include <interactive_markers/menu_handler.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <ros/param.h>
#include <fstream>
#include <cmath>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
using namespace visualization_msgs;
using namespace geometry_msgs;
using namespace std;
using namespace boost;
class IMS{
boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
IMS();
void doNothing(const InteractiveMarkerFeedbackConstPtr &feedback);
void testServer();
int main();
};
菲律宾共产党声明
(文件名为 "ims.cpp")
#include <ims.h>
IMS::IMS(){}
void IMS::doNothing(const InteractiveMarkerFeedbackConstPtr &feedback){
}
void IMS::testServer(){
InteractiveMarker inter_marker;
inter_marker.header.frame_id = 1;
Point pos;
pos.x = 3;
pos.y = 3;
inter_marker.pose.position = pos;
inter_marker.scale = 2;
inter_marker.name = "testServer";
server->insert(inter_marker, &IMS::doNothing); //other options that didn't work either: doNothing);//*this->doNothing(const InteractiveMarkerFeedbackConstPtr &feedback));//*this->doNothing());//this->doNothing);//&this->doNothing);//&IMS::doNothing);//&doNothing);
}
int IMS::main(){}
抛出错误:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
from /usr/include/boost/function.hpp:64,
from /opt/ros/indigo/include/ros/forwards.h:40,
from /opt/ros/indigo/include/ros/common.h:37,
from /opt/ros/indigo/include/ros/ros.h:43,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/include/ims.h:3,
from /home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:1:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::function_void_mem_invoker1<MemberPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with MemberPtr = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’:
/usr/include/boost/function/function_template.hpp:934:38: required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&]’
/usr/include/boost/function/function_template.hpp:722:7: required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1069:16: required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (IMS::*)(const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&); R = void; T0 = const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/home/ros/ros/src/robotrainer_editor/heika_beta/ims/src/ims.cpp:19:47: required from here
/usr/include/boost/function/function_template.hpp:225:11: error: no match for call to ‘(boost::_mfi::mf1<void, IMS, const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&>) (const boost::shared_ptr<const visualization_msgs::InteractiveMarkerFeedback_<std::allocator<void> > >&)’
BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
诊断:
我的一个朋友实际上已经诊断出问题的可能来源:显然编译器将代码重新格式化为这个
void IMS::doNothing(IMS this, const InteractiveMarkerFeedbackConstPtr &feedback){
这当然会导致错误,因为参数不再符合我们的预期。
所以这是我的要求:
这个诊断是否正确?
如果是:可以解决吗?如何解决?
else: 实际问题是什么?
感谢所有完整阅读这篇长文章的人,并提前感谢您的回答!
解决方案(根据"einpoklum"的建议)
最符合我的想法的解决方案是 lambda,创建一个函数来包装恶意 "this" 参数。
std::function nothing = [this] (const InteractiveMarkerFeedbackConstPtr &feedback) {this->doNothing(feedback);};
server->insert(inter_marker, nothing);
非常感谢您和所有费心阅读整个主题的人,对于我难以理解地提出问题感到抱歉。
试图穿过堆积如山的文字并冒险回答:我认为(但不确定)问题是你试图通过一个(非静态)成员函数,就好像它是一个独立的函数一样。
那应该行不通,因为您不能调用成员函数 "just like that" - 它必须有一个关联的对象。在实现层面上,需要用 class 实例的地址调用该段代码,以作为 this
对象。
在这些情况下您可以做的是:
- 使用
std::mem_fn
包装您的成员函数以获得适当的函数,额外的第一个参数是实例,或者 - 使用 lambda 以某种方式实例化对象(或仅将实例作为引用)并调用该实例的方法。 lambda 本身将退化为一个独立函数,您可以传递其指针。
- 创建方法
static
- 如果它实际上不使用任何特定于实例的数据。