C++ 引用作为类成员的函数

C++ 引用作为类成员的函数,c++,pointers,lambda,types,pass-by-reference,C++,Pointers,Lambda,Types,Pass By Reference,所以我有一个意大利面代码项目,我正试图使其面向对象。值得一提的是,我遇到了一些我不太理解的错误,所以我尝试创建了一些简单的代码,这些代码确实会抛出相同的错误,但事实并非如此 下面是一些编译的简约代码: (文件名为“ims.cpp”) 这当然会导致错误,因为参数不再符合我们的期望 我的请求如下: 这个诊断正确吗 如果是:是否可以采取变通办法?如何解决 否则:实际问题是什么 感谢所有阅读这篇长文章的人,并提前感谢你们的回答 解决方案(根据“einpoklum”的建议) 最适合我概念的解决方案是

所以我有一个意大利面代码项目,我正试图使其面向对象。值得一提的是,我遇到了一些我不太理解的错误,所以我尝试创建了一些简单的代码,这些代码确实会抛出相同的错误,但事实并非如此


下面是一些编译的简约代码:

(文件名为“ims.cpp”)

这当然会导致错误,因为参数不再符合我们的期望


我的请求如下:

这个诊断正确吗

如果是:是否可以采取变通办法?如何解决

否则:实际问题是什么

感谢所有阅读这篇长文章的人,并提前感谢你们的回答


解决方案(根据“einpoklum”的建议)

最适合我概念的解决方案是lambda,它创建了一个函数来包装恶意的“this”参数

std::函数无= [this](const InteractiveMarkerFeedbackConstPtr&feedback){this->doNothing(feedback);}

服务器->插入(中间标记,无)


非常感谢您和所有费心阅读全文的人,很抱歉我很难理解地提出这个问题。

试图穿过堆积如山的文本并大胆回答:我认为(但不确定)问题在于您试图通过a(非静态)成员功能,就好像它是一个独立的功能。

这不应该起作用,因为不能“像那样”调用成员函数——它必须有一个关联的对象。在实现级别上,需要使用类实例的地址调用该代码段,以用作
对象

在这些情况下,您可以做的是:

  • 使用包装您的成员函数以获得适当的函数,并使用额外的第一个参数作为实例,或
  • 使用lambda以某种方式实例化对象(或仅将实例作为引用)并调用该实例的方法。lambda本身将退化为一个独立函数,您可以传递该函数的指针
  • 如果方法实际上没有使用任何特定于实例的数据,则将其设置为静态

doNothing
应该是
static
,(
static void doNothing(const InteractiveMarkerFeedbackConstPtr&feedback);
在类定义中)<代码>静态函数没有这个,@Kelvin Sherlock然后会出现一个问题,就是(真实的)函数(真实项目的,一个做某事的函数)实际上使用了类属性,而它当时不能,不是吗?还有别的办法吗?@GDawg请看你是我的英雄<代码>标准::函数nothing=[this](const InteractiveMarkerFeedbackConstPtr&feedback){this->doNothing(feedback);}
server->insert(inter_标记和IMS::doNothing)@GDawg:1。如果你发现解决方案有帮助,请考虑投票。2.没有“e”的“英雄”:-)3。要通读你的问题是非常困难的,你应该更加努力地提供最少的例子,我们可以自己编译,看看会发生什么。4.这里列出的两个语句是不相关的,即第二个语句没有使用您定义的lambda;也许你复制错了?sry,upvote暂停,新的堆栈溢出。关于最小代码:抱歉,我没有找到任何与StART C++方法相同的问题^ ^,来测试你需要在我的特定版本中安装ROS的代码等等,只是希望有人能理解这个理论概念,你做到了!服务器->插入(中间标记,无)*英雄
#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(){}
#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));
#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();

};
#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){