我是一个C++新手,我试图通过UART发布来自ARM芯片组的传感器数据,并使用 ROSserial_stm32 原有的源头来自于一个用C语言编写的演示代码,它收集数据并以 "字符串格式 "通过UART发送。我的目的是取代那些 发送串 到 ROS传感器_msgs 框架格式。
为了实现这一目标,我创建了以下 头文件:
/*
* ROSserial.h
*
* Created on: May 3, 2020
* Author: fofolevrai
*/
#ifndef INC_ROSSERIAL_H_
#define INC_ROSSERIAL_H_
#include <ros.h>
#include <geometry_msgs/Vector3.h>
#include <sensor_msgs/FluidPressure.h>
#include <sensor_msgs/Temperature.h>
#include <sensor_msgs/RelativeHumidity.h>
class ROSserial
{
private:
// Node handler
ros::NodeHandle nh;
// Temperature data
sensor_msgs::Temperature *lps22hb_air_temperature_t_;
public:
// Temperature publisher/topic
ros::Publisher lps22hb_air_temperature_publisher_t;
// Class constructor
ROSserial(void);
// Methods
void Init(void);
};
#endif /* INC_ROSSERIAL_H_ */
及其相关的 C++源文件:
/*
* ROSserial.cpp
*
* Created on: May 4, 2020
* Author: fofolevrai
*/
#include "ROSserial.h"
// [ISSUE 1 ON FOLLOWING]
ROSserial::ROSserial(void)
{
this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}
void ROSserial::Init(void)
{
// Initialize ROS publisher [ISSUE 2 ON FOLLOWING]
this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);
// Initialize ROS node
this->nh.initNode();
this->nh.advertise(lps22hb_air_temperature_publisher_t);
}
但是,我得到了以下错误。
问题1
../Core/Src/ROSserial.cpp: In constructor 'ROSserial::ROSserial()':
../Core/Src/ROSserial.cpp:9:26: error: no matching function for call to 'ros::Publisher::Publisher()'
ROSserial::ROSserial(void)
第2期
../Core/Src/ROSserial.cpp: In member function 'void ROSserial::Init()':
../Core/Src/ROSserial.cpp:17:99: error: no match for call to '(ros::Publisher) (const char [20], sensor_msgs::Temperature*&)'
this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);
我在网上转了好几天了 (1) (2) (3)、循环和重新操纵代码,还是不明白是什么问题?我真的很希望能得到编译器输出的帮助?
谢谢你的帮助。
当你不在构造函数中使用初始化器列表初始化成员变量(和基类)时,它们是默认初始化的。即
ROSserial::ROSserial(void)
{
this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}
相当于
ROSserial::ROSserial(void) : nh(),
lps22hb_air_temperature_t_(),
lps22hb_air_temperature_publisher_t()
{
this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}
编译器的错误信息表明 lps22hb_air_temperature_publisher_t
不能被默认初始化。因此,需要对其进行适当的初始化。
this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);
这种语法在构造函数的初始化列表中初始化一个成员是有效的,但这不是设置对象值的正确语法。你可以使用
this->lps22hb_air_temperature_publisher_t = ros::Publisher("LPS22HB_Temperature", this->lps22hb_air_temperature_t_));
的值来设置 lps22hb_air_temperature_publisher_t
.
您可以通过以下方式解决这两个问题。
Init
成员功能和 class ROSserial
{
private:
// Node handler
ros::NodeHandle nh;
// Temperature data
sensor_msgs::Temperature *lps22hb_air_temperature_t_;
// Constructor, for private usage.
ROSserial(std::string const& desc,
sensor_msgs::Temperature* temp);
public:
// Temperature publisher/topic
ros::Publisher lps22hb_air_temperature_publisher_t;
// Class constructor
ROSserial();
};
ROSserial::ROSserial(std::string const& desc,
sensor_msgs::Temperature* temp) :
lps22hb_air_temperature_t_(temp),
lps22hb_air_temperature_publisher_t(desc, temp)
{
// Initialize ROS node
this->nh.initNode();
this->nh.advertise(lps22hb_air_temperature_publisher_t);
}
// Use delegating constructor.
ROSserial::ROSserial() : ROSserial("LPS22HB_Temperature", new sensor_msgs::Temperature())
{
}