构造函数中没有匹配的函数调用
No matching function call on constuctor
我是 C++ 的新手,尝试使用 ROSserial_stm32 框架通过 UART 从 ARM 芯片组发布传感器数据。
原始资源来自用 C 编写的演示代码,该代码收集数据并在 "string format" 中通过 UART 发送。我的目标是将那些 string sendings 替换为 ROS sensor_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),循环并重新操作代码,但仍然无法理解哪里出了问题?我真的很感激编译器输出的帮助?
感谢您的帮助,
第 1 期
当您不在构造函数中使用初始化列表初始化成员变量(和基 类)时,它们将被默认初始化。即
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
不能默认初始化。因此,需要对其进行适当的初始化。
第 2 期
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())
{
}
我是 C++ 的新手,尝试使用 ROSserial_stm32 框架通过 UART 从 ARM 芯片组发布传感器数据。 原始资源来自用 C 编写的演示代码,该代码收集数据并在 "string format" 中通过 UART 发送。我的目标是将那些 string sendings 替换为 ROS sensor_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),循环并重新操作代码,但仍然无法理解哪里出了问题?我真的很感激编译器输出的帮助?
感谢您的帮助,
第 1 期
当您不在构造函数中使用初始化列表初始化成员变量(和基 类)时,它们将被默认初始化。即
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
不能默认初始化。因此,需要对其进行适当的初始化。
第 2 期
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())
{
}