gpt4 book ai didi

C++没有匹配函数调用 `LogBlock`

转载 作者:行者123 更新时间:2023-12-02 10:58:42 28 4
gpt4 key购买 nike

我正在尝试修改 already existing package of ROS ,在 crazyflie_driver 文件夹中添加和删除一些东西。我正在编译 catkin_make我有 ROS Kinetic 16.04。
我实际修改的 crazyflie_server C++ 脚本的部分是这样的:
私有(private)的:
结构 logImu {
float acc_x;
float acc_y;
float acc_z;
float 陀螺_x;
float 陀螺仪;
float 陀螺z;
} 属性 ((包装));

private:
struct logMotor {
int m1;
int m2;
int m3;
int m4;
} __attribute__((packed));


void onImuData(uint32_t time_in_ms, logImu* data) {
if (m_enable_logging_imu) {
sensor_msgs::Imu msg;
if (m_use_ros_time) {
msg.header.stamp = ros::Time::now();
} else {
msg.header.stamp = ros::Time(time_in_ms / 1000.0);
}
msg.header.frame_id = m_tf_prefix + "/base_link";
msg.orientation_covariance[0] = -1;
//new
msg.orientation.x = cos(data->gyro_z/2)*cos(data->gyro_y/2)*cos(data->gyro_x/2) + sin(data->gyro_z/2)*sin(data->gyro_y/2)*sin(data->gyro_x/2);
msg.orientation.y = sin(data->gyro_z/2)*cos(data->gyro_y/2)*cos(data->gyro_x/2) - cos(data->gyro_z/2)*sin(data->gyro_y/2)*sin(data->gyro_x/2);
msg.orientation.z = cos(data->gyro_z/2)*sin(data->gyro_y/2)*cos(data->gyro_x/2) + sin(data->gyro_z/2)*cos(data->gyro_y/2)*sin(data->gyro_x/2);
msg.orientation.w = cos(data->gyro_z/2)*cos(data->gyro_y/2)*sin(data->gyro_x/2) - sin(data->gyro_z/2)*sin(data->gyro_y/2)*cos(data->gyro_x/2);

//endnew


// measured in deg/s; need to convert to rad/s
msg.angular_velocity.x = degToRad(data->gyro_x);
msg.angular_velocity.y = degToRad(data->gyro_y);
msg.angular_velocity.z = degToRad(data->gyro_z);

// measured in mG; need to convert to m/s^2
msg.linear_acceleration.x = data->acc_x * 9.81;
msg.linear_acceleration.y = data->acc_y * 9.81;
msg.linear_acceleration.z = data->acc_z * 9.81;

m_pubImu.publish(msg);
}
}

void onMotorData(logMotor* data) {
std_msgs::Int32MultiArray array;
array.data.clear();
array.data.push_back(data->m1);
array.data.push_back(data->m2);
array.data.push_back(data->m3);
array.data.push_back(data->m4);

m_pubMotors.publish(array);
}

std::function<void(uint32_t, logImu*)> cb = std::bind(&CrazyflieROS::onImuData, this, std::placeholders::_1,std::placeholders::_2);

logBlockImu.reset(new LogBlock<logImu>(
&m_cf,{
{"acc", "x"},
{"acc", "y"},
{"acc", "z"},
{"gyro", "x"},
{"gyro", "y"},
{"gyro", "z"},
}, cb));
logBlockImu->start(1); // 10ms

std::function<void(logMotor*)> cbmotor = std::bind(&CrazyflieROS::**onMotorData**, this, std::placeholders::_1);
logBlockMotor.reset(new LogBlock<logMotor>(
&m_cf,{
{"motor", "m1"},
{"motor", "m2"},
{"motor", "m3"},
{"motor", "m4"},
}, cbmotor));


logBlockMotor->start(1); // 10ms


}

我的问题是,当我尝试使用这部分代码进行编译时:
>logBlockMotor.reset(new LogBlock<logMotor>(
&m_cf,{
{"motor", "m1"},
{"motor", "m2"},
{"motor", "m3"},
{"motor", "m4"},
}, cbmotor));

它给了我一个不匹配的函数来调用 LogBlock<CrazyflieROS::logMotor>
    /home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_driver/src/crazyflie_server.cpp: In member function ‘void CrazyflieROS::run()’:
/home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_driver/src/crazyflie_server.cpp:530:9: error: no matching function for call to ‘LogBlock<CrazyflieROS::logMotor>::LogBlock(Crazyflie*, <brace-enclosed initializer list>, std::function<void(unsigned int, CrazyflieROS::logImu*)>&)’
}, cb));
^
In file included from /home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_driver/src/crazyflie_server.cpp:50:0:
/home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_cpp/include/crazyflie_cpp/Crazyflie.h:462:3: note: candidate: LogBlock<T>::LogBlock(Crazyflie*, std::__cxx11::list<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, std::function<void(unsigned int, T*)>&) [with T = CrazyflieROS::logMotor]
LogBlock(
^
/home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_cpp/include/crazyflie_cpp/Crazyflie.h:462:3: note: no known conversion for argument 3 from ‘std::function<void(unsigned int, CrazyflieROS::logImu*)>’ to ‘std::function<void(unsigned int, CrazyflieROS::logMotor*)>&’
/home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_cpp/include/crazyflie_cpp/Crazyflie.h:459:7: note: candidate: LogBlock<CrazyflieROS::logMotor>::LogBlock(const LogBlock<CrazyflieROS::logMotor>&)
class LogBlock
^
/home/bitcraze/catkin_ws/src/crazyflie_ros/crazyflie_cpp/include/crazyflie_cpp/Crazyflie.h:459:7: note: candidate expects 1 argument, 3 provided
crazyflie_ros/crazyflie_driver/CMakeFiles/crazyflie_server.dir/build.make:62: recipe for target 'crazyflie_ros/crazyflie_driver/CMakeFiles/crazyflie_server.dir/src/crazyflie_server.cpp.o' failed
make[2]: *** [crazyflie_ros/crazyflie_driver/CMakeFiles/crazyflie_server.dir/src/crazyflie_server.cpp.o] Error 1
CMakeFiles/Makefile2:2995: recipe for target 'crazyflie_ros/crazyflie_driver/CMakeFiles/crazyflie_server.dir/all' failed
make[1]: *** [crazyflie_ros/crazyflie_driver/CMakeFiles/crazyflie_server.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j1 -l1" failed

这是 LogBlock.h 文件的一部分:
namespace crazyflie_driver
{
template <class ContainerAllocator>
struct LogBlock_
{
typedef LogBlock_<ContainerAllocator> Type;

LogBlock_()
: topic_name()
, frequency(0)
, variables() {
}
LogBlock_(const ContainerAllocator& _alloc)
: topic_name(_alloc)
, frequency(0)
, variables(_alloc) {
(void)_alloc;
}



typedef std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > _topic_name_type;
_topic_name_type topic_name;

typedef int16_t _frequency_type;
_frequency_type frequency;

typedef std::vector<std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > , typename ContainerAllocator::template rebind<std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > >::other > _variables_type;
_variables_type variables;





typedef boost::shared_ptr< ::crazyflie_driver::LogBlock_<ContainerAllocator> > Ptr;
typedef boost::shared_ptr< ::crazyflie_driver::LogBlock_<ContainerAllocator> const> ConstPtr;

}; // struct LogBlock_

我不明白我做错了什么,也是因为如果我在代码中留下 onImuData 的东西,它编译得很好。一旦我添加了那部分代码,就会出现编译器错误。

谁能帮我?我还尝试查看 CMake.txt 文件,但它似乎包含目录并且没有任何问题:
cmake_minimum_required(VERSION 2.8.3)
project(crazyflie_driver)

find_package(catkin REQUIRED COMPONENTS
message_generation
std_msgs
tf
crazyflie_cpp
)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

add_service_files(
FILES
AddCrazyflie.srv
GoTo.srv
Land.srv
RemoveCrazyflie.srv
SetGroupMask.srv
StartTrajectory.srv
Stop.srv
Takeoff.srv
UpdateParams.srv
UploadTrajectory.srv
sendPacket.srv
)

add_message_files(
FILES
LogBlock.msg
GenericLogData.msg
FullState.msg
TrajectoryPolynomialPiece.msg
crtpPacket.msg
Hover.msg
Position.msg
)

## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
geometry_msgs
)

catkin_package(
CATKIN_DEPENDS
message_runtime
std_msgs
tf
crazyflie_cpp
)

###########
## Build ##
###########

include_directories(
${catkin_INCLUDE_DIRS}
)

## Declare a cpp executable
add_executable(crazyflie_server
src/crazyflie_server.cpp
)

add_dependencies(crazyflie_server
crazyflie_driver_generate_messages_cpp
)

## Specify libraries to link a library or executable target against
target_link_libraries(crazyflie_server
${catkin_LIBRARIES}
)

## Declare a cpp executable
add_executable(crazyflie_add
src/crazyflie_add.cpp
)

add_dependencies(crazyflie_add
crazyflie_driver_generate_messages_cpp
)

target_link_libraries(crazyflie_add
${catkin_LIBRARIES}
)

先感谢您!

最佳答案

你正在创造

std::function<void(logMotor*)> cbmotor = ...

然后将它传递给期望的构造函数
std::function<void(uint32_t, T*)>&

(其中 T 大概是 logMotor )。这两种类型不兼容。这是我在您的来源中读到的。

另一方面,错误消息说

no known conversion for argument 3 from ‘std::function<void(unsigned int, CrazyflieROS::logImu*)>’ to ‘std::function<void(unsigned int, CrazyflieROS::logMotor*)>&’



这意味着您正在创建 logImu面向回调并试图将其传递给 logMotor定向参数。您可能会显示来自与您作为源发布的文件不同的文件的错误消息。

在任何情况下,您都需要传递正确类型的回调。

在一个有点不相关的注释上:
  LogBlock(
Crazyflie* cf,
std::list<std::pair<std::string, std::string> > variables,
std::function<void(uint32_t, T*)>& callback)

是您尝试使用的包中的错误。有一个非常量引用是没有意义的 std::function参数,然后立即将其复制到成员变量。这不必要地将构造函数限制为仅与非常量左值参数一起使用。例如,你可以做
std::function<void(uint32_t, logImu*)> cb = std::bind(...);

new LogBlock<logImu>(..., ..., cb)

但你不能直接做
    new LogBlock<logImu>(..., ..., std::bind (...))

构造函数应该有一个 const 引用参数,并且可以选择有一个带有右值引用参数的重载。

该函数还接受 std::list按值(value)计算,这也没有任何意义。

关于C++没有匹配函数调用 `LogBlock<CrazyflieROS::logMotor>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51317144/

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