引用属于类

时间:2017-07-14 12:57:37

标签: c++ pointers lambda types pass-by-reference

所以我有这个意大利面条代码项目,我试图使面向对象。 Pittily我遇到了一些我不太了解的错误,所以我尝试创建简单的代码,它会抛出相同的错误而且没有。

所以这里有一些编译的简约代码:

(文件名为" 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(){}

Explenation:这是一个ROS(机器人操作系统)项目,我仍然相信这是一个普遍的c ++问题所以我没有在他们的&#34; ros :: answers&#34;中提出这个问题。论坛。请不要对类型感到困惑,我们会遇到问题。

功能&#34; interactive_markers :: InteractiveMarkerServer.insert&#34;需要一个&#34; visualization_msgs :: InteractiveMarker&amp;&#34;和一个参数类型为&#34; InteractiveMarkerFeedbackConstPtr&amp;&#34;的函数,如提供的那样。 见:http://docs.ros.org/jade/api/interactive_markers /html/classinteractive__markers_1_1InteractiveMarkerServer.html

因此,抛出我的错误的最小代码将是一个在&#34; doNothing&#34;中没有所需参数的代码。功能,像这样:

(文件名为&#34; ims.cpp&#34;)

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

哪个有道理,对吧?该函数没有所需的参数,因此编译器会抱怨。

现在我们在面向对象的代码中遇到了真正的问题:,我遇到了同样的问题:

HEADER FILE

(文件名为&#34; ims.h&#34;)

#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();

};

CPP声明

(文件名为&#34; ims.cpp&#34;)

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

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

所以这是我的要求:

这种诊断是否正确?

如果是的话:它可以是变通方法吗?

否则:实际问题是什么?

感谢所有甚至完全阅读这篇长篇帖子的人,并提前感谢您的答案!

解决方案(遵循&#34; einpoklum&#34的建议;)

对我的概念最合适的解决方案是一个lambda,创建一个功能来包装恶意&#34;这个&#34;参数。

  

std :: function nothing =   [this](const InteractiveMarkerFeedbackConstPtr&amp; feedback){this-&gt; doNothing(feedback);};

     

server-&gt; insert(inter_marker,nothing);

非常感谢那些非常厌烦阅读整个帖子的人,对不起,我很难理解这个问题。

1 个答案:

答案 0 :(得分:-1)

尝试切入大量文本并冒险回答:我(但不确定)问题是你试图将(非静态)成员函数传递为虽然这是一个独立的功能。

这不应该起作用,因为你无法调用成员函数&#34;就像那样&#34; - 它必须有一个相关的对象。在实现级别上,需要使用类的实例的地址调用该段代码作为this对象。

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

  • 使用std::mem_fn包裹您的成员函数以获得正确的函数,并使用额外的第一个参数作为实例,或
  • 使用lambda以某种方式实例化对象(或者仅将实例作为引用)并调用该实例的方法。 lambda本身将退化为一个独立的函数,它可以传递指针。
  • 制作方法static - 如果它实际上并未使用任何特定于实例的数据。