Building error when declare tf2_ros::Buffer in class - c++

I am trying to use tf2_ros::Buffer in a simple code. When I put it in the main function, everything works fine. But when put in a class, building error occurs. The code is like this:
#include <ros/ros.h>
#include <tf2_ros/buffer.h>
#include <tf2_ros/transform_listener.h>
#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Twist.h>
class test_class
{
private:
double start;
double duration;
ros::Time start_time;
ros::Time end_time;
std::string robot_name;
tf2_ros::Buffer tf_buffer; // problem line
tf2_ros::TransformListener* tfListener;
geometry_msgs::TransformStamped transformStamped;
public:
std::string space_name;
std::string node_name;
test_class()
{
space_name = ros::this_node::getNamespace();
node_name = ros::this_node::getName();
}
~test_class()
{}
bool initialize(const ros::NodeHandle& n)
{
ROS_INFO("Class auto_mav_flight initialized done!");
return true;
}
void timer_callback(const ros::TimerEvent& event)
{
ROS_INFO("Timer Callback triggered.");
return;
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "auto_mav_node");
ros::NodeHandle node;
ROS_WARN("The node is initilized and started.");
test_class amf = test_class();
amf.initialize(node);
ros::Timer timer_1 = node.createTimer(ros::Duration(0.5), &test_class::timer_callback, &amf);
ros::spin();
return EXIT_SUCCESS;
}
and the building error information is:
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp: In function ‘int main(int, char**)’:
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:73:44: error: no matching function for call to ‘test_class::test_class(test_class)’
test_class amf = test_class();
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:73:44: note: candidates are:
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2: note: test_class::test_class()
test_class()
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2: note: candidate expects 0 arguments, 1 provided
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:9:7: note: test_class::test_class(test_class&)
class test_class
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:9:7: note: no known conversion for argument 1 from ‘test_class’ to ‘test_class&’
make[2]: *** [auto_mav_flight/CMakeFiles/auto_mav_flight_node.dir/src/node_main.cpp.o] Error 1
make[1]: *** [auto_mav_flight/CMakeFiles/auto_mav_flight_node.dir/all] Error 2
make: *** [all] Error 2
I found that if I comment the code line that declare the tf2_ros::buffer:
tf2_ros::Buffer tf_buffer;
the error disappear.
Why the tf2_ros::Buffer can cause problem of class construction even I just declare it as a member of class?
Any help will be appreciated.
Thanks in advance.

From this :
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2:
note: candidate expects 0 arguments, 1 provided
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:9:7:
note: test_class::test_class(test_class&)
It appears you are calling the copy constructor of test_class (could be hidden in the layer of ROS, by trying to pass test_class as a function arguments or when using containers).
From tf2_ros::Buffer header, it inherits from BufferCore, which contains a boost::mutex(among other things - there could more than 1 non-copyable attribute) which is not copy-constructible. That makes tf2_ros::Buffer not copy-constructible.
Since test_class do not define a copy-constructor and contains a non-copyable attribute, the compiler cannot generate a copy constructor and fails to compile when you try to call a copy constructor.
For reference :
http://en.cppreference.com/w/cpp/language/copy_constructor

Related

c++ error : no matching function for call to

i am a newbie in c++. the source codes are taken from github.com/srsran/srsRAN. i am trying to call a function from a class nas_5g. this class is inside namespace srsue as shown in the header file below.
header file nas_5g.h
#include "srsue/hdr/stack/upper/nas_5g.h"
namespace srsue {
class nas_5g : public nas_base, public nas_5g_interface_rrc_nr, public nas_5g_interface_procedures
{
public:
explicit nas_5g(srslog::basic_logger& logger_, srsran::task_sched_handle task_sched_);
virtual ~nas_5g();
int init(usim_interface_nas* usim_,
rrc_nr_interface_nas_5g* rrc_nr_,
gw_interface_nas* gw_,
const nas_5g_args_t& cfg_);
void stop();
void run_tti();
bool is_registered();
int get_k_amf(srsran::as_key_t& k_amf);
uint32_t get_ul_nas_count();
int write_pdu(srsran::unique_byte_buffer_t pdu);
int switch_off();
}
source file nas_5g.cc
using namespace srsran;
using namespace srsran::nas_5g;
namespace srsue {
nas_5g::nas_5g(srslog::basic_logger& logger_, srsran::task_sched_handle task_sched_) :
nas_base(logger_, MAC_5G_OFFSET, SEQ_5G_OFFSET, NAS_5G_BEARER),
task_sched(task_sched_),
reregistration_timer(task_sched_.get_unique_timer()),
registration_proc(this),
state(logger_),
pdu_session_establishment_proc(this, logger_)
{
// Configure timers
}
nas_5g::~nas_5g() {}
int nas_5g::switch_off()
{
logger.info("Switching off");
send_deregistration_request_ue_originating(true);
return SRSRAN_SUCCESS;
}
i tried to define the object and call function switch_off() in the main.cc file as shown below
main.cc
#include "srsue/hdr/stack/upper/nas_5g.h"
using namespace srsue;
int main(int argc, char* argv[])
{
srsue::nas_5g nas5g;
nas5g.switch_off();
}
when compiling the source codes, i received below error:-
/srsRAN/srsue/src/main.cc: In function ‘int main(int, char**)’:
/srsRAN/srsue/src/main.cc:823:17: error: no matching function for call to ‘srsue::nas_5g::nas_5g()’
823 | srsue::nas_5g nas5g;
| ^~~~~
In file included from /srsRAN/srsue/src/main.cc:50:
/srsRAN/srsue/hdr/stack/upper/nas_5g.h:59:12: note: candidate: ‘srsue::nas_5g::nas_5g(srslog::basic_logger&, srsran::task_sched_handle)’
59 | explicit nas_5g(srslog::basic_logger& logger_, srsran::task_sched_handle task_sched_);
| ^~~~~~
/srsRAN/srsue/hdr/stack/upper/nas_5g.h:59:12: note: candidate expects 2 arguments, 0 provided
make[2]: *** [srsue/src/CMakeFiles/srsue.dir/build.make:63: srsue/src/CMakeFiles/srsue.dir/main.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:11439: srsue/src/CMakeFiles/srsue.dir/all] Error 2
make: *** [Makefile:163: all] Error 2
appreciate if anyone could shed some light. thanks in advance!

Issue passing a reference to another class within a class

I have created the following class which has an object being passed in by reference and is implemented the following way.
Diablo_Serial_4DLib Display(&DisplaySerial);
ZenDisplay ui(Display);
class ZenDisplay
{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib* _display;
};
The constructor is straight forward and works as expected, no problem so far.
// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display)
{
_display = &display;
}
I want to instantiate ZenSpeakerGroup class and pass in the same reference into it's constructor
class ZenSpeakerGroup
{
public:
ZenSpeakerGroup(Diablo_Serial_4DLib &display);
private:
Diablo_Serial_4DLib* _display;
};
ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib &display)
{
_display = &display;
}
I have modified the original working class to the following
class ZenDisplay
{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib* _display;
ZenSpeakerGroup _speakerGroup;
};
// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
{
_display = &display;
}
Now I get the following error and not 100% sure what I am doing wrong.
Arduino: 1.8.16 (Mac OS X), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None"
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp: In constructor 'ZenDisplay::ZenDisplay(Diablo_Serial_4DLib&)':
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:14:78: error: no matching function for call to 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib*)'
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
^
In file included from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.h:14,
from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:9:
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note: candidate: 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib&)'
ZenSpeakerGroup(Diablo_Serial_4DLib &display);
^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'Diablo_Serial_4DLib&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(const ZenSpeakerGroup&)'
class ZenSpeakerGroup
^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'const ZenSpeakerGroup&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(ZenSpeakerGroup&&)'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'ZenSpeakerGroup&&'
exit status 1
Error compiling for board DOIT ESP32 DEVKIT V1.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I also tried the following with no success.
_speakerGroup = new ZenSpeakerGroup(&display)
I would appreciate some help on this.
This is the correct way of passing the display reference to ZenSpeakerGroup
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(display)
{
_display = &display;
}
I have made this mistake aswell it's a typo
make sure when you want to intialize a reference define it as a refference
class ZenDisplay{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib& _display; // <--make sure this is a refference
};
now initialize the refference
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display){
_display = display;
}

cpp no matching function call for call to constructor. Why?

Below is my code:
// this code illustrates iterating through a nested hashmap.
#include <iostream>
#include "imported.hpp"
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
#define MAX_LINE_LENGTH 999
using namespace std;
class State
{
public:
vector<string> vec;
string state_string;
State(string state_string, vector<string> vec);
};
State::State(string state_string, vector<string> vec)
{
this->state_string = state_string;
this->vec = vec;
}
class Heuristic
{
public:
State goal_state;
string type;
Heuristic(string type, State goal_state);
};
Heuristic::Heuristic(string type, State goal_state)
{
this->type = type;
this->goal_state = goal_state;
}
int main(int argc, char const *argv[])
{
}
When I try to compile it using:
g++ filename.cpp
The following output is produced:
$ g++ main.cpp
main.cpp: In constructor ‘Heuristic::Heuristic(std::string, State)’:
main.cpp:36:51: error: no matching function for call to ‘State::State()’
Heuristic::Heuristic(string type, State goal_state)
^
main.cpp:21:1: note: candidate: State::State(std::string, std::vector<std::basic_string<char> >)
State::State(string state_string, vector<string> vec)
^~~~~
main.cpp:21:1: note: candidate expects 2 arguments, 0 provided
main.cpp:12:7: note: candidate: State::State(const State&)
class State
^~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: State::State(State&&)
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
I am confused as to why this is happening since I am not even calling the constructor but rather am defining a function's method signature into which the user should be able to pass an existent State object. Please assist.
The Heuristic constructor is built using assignment operators which involves the default construction of its member objects. Since State does not have a default constructor, this form of construction will fail.
There are two ways of solving this:
If the members have user-defined constructors, provide default-constructors also for them .
Use initializer lists for your constructor instead of assignments within the body of the constructor.
Of these two methods, the second one is more preferable. The reasons for this are outlined in the FAQ: Should my constructors use “initialization lists” or “assignment”?

invincibility frame and take damage class

i am a beginner and i have already read a doc but i need to practice for learn and now i am stuck.
So i would like to do a class takeDommage for apply a number of dmg and activate a countdown for create the invincibility frame.
so i tryed this (see code under)
It's the first class i create alone so i don't understand what's wrong
main :
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
takeDommage::prendreDegat(10);
std::cout<<pv<<std::endl;
}
takeDommage.h :
#ifndef TAKEDOMMAGE_H
#define TAKEDOMMAGE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
class takeDommage
{
public:
takeDommage();
prendreDegat(int Dommage);
//virtual ~takeDommage();
protected:
sf::Clock takeDammageClock;
int Dommage;
private:
};
#endif // TAKEDOMMAGE_H
takeDommage.cpp
#include "takeDommage.h"
takeDommage::takeDommage()
{
}
void takeDommage::prendreDegat(int Dommage)
{
if(takeDammageClock.getElapsedTime().asSeconds()>=3)
{
std::cout << "bite" << std::endl;
pv -= Dommage;
takeDammageClock.restart();
}
}
error:
||=== Build: Debug in TheGameSFML (compiler: GNU GCC Compiler) ===|
E:\Work\Top_secret\code\TheGame\main.cpp||In function 'int main()':|
E:\Work\Top_secret\code\TheGame\main.cpp|168|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|180|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|217|error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
E:\Work\Top_secret\code\TheGame\main.cpp|156|warning: unused variable 'enemySpawnTimer' [-Wunused-variable]|
E:\Work\Top_secret\code\TheGame\src\takeDommage.cpp|8|error: prototype for 'void takeDommage::prendreDegat(int)' does not match any in class 'takeDommage'|
include\takeDommage.h|15|error: candidate is: int takeDommage::prendreDegat(int)|
||=== Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
you seem to be very new in c++. the compiler already telling you what's wrong with it.
error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
you need to instantiate (create) your object first. the way you accessing as if the takeDommage function is a static function which is not. its public a member function of takeDamage
assuming that you have instantiated your takeDommage class somewhere before the if statement call,
TakeDommage Obj;
...
...
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
Obj.prendreDegat(10); //calling the prendredegat member function of Obj
std::cout<<pv<<std::endl;
}
in addition to that you are missing the return type void before the function name of prendreDegat
class takeDommage
{
public:
takeDommage();
//prendreDegat(int Dommage); //missing void
void prendreDegat(int Dommage); //correct way. which can be translated as Prendredegat returns nothing.

C++ can't write constructor for child class

Problem solved! Thank you for help.
I'm writing scheduler for my program. I have got 2 classes: task_timepoint and task_period. task_period is a derived class of task_timepoint.
I wrote constructor for task_timepoint, but when I start writing constructor for task_period I've got errors that I can't solve.
scheduler.hpp:
#ifndef SCHEDULER_H
#define SCHEDULER_H
#include "boost/date_time/posix_time/posix_time.hpp"
#include <functional>
#include <chrono>
namespace schelduler{
using namespace schelduler;
using namespace boost::posix_time;
class task_timepoint{
protected:
ptime run_time; //Time at(after) that task_function should run
std::function<void(void)> task_function; //Function that should be run at specified time
public:
task_timepoint(ptime run_time, std::function<void(void)> task_function);
};
class task_period : public task_timepoint{
private:
time_duration run_period;
public:
task_period(time_duration run_period, std::function<void(void)> task_function);
};
}
#endif
scheduler.cpp:
#include "scheduler.hpp"
using namespace schelduler;
using namespace boost::posix_time;
task_timepoint::task_timepoint(ptime run_time, std::function<void(void)> task_function)
{
task_timepoint::run_time = run_time;
task_timepoint::task_function = task_function;
}
task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
{
this->run_period = run_period;
//task_period::task_function = task_function;
}
Errors:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp: In constructor ‘schelduler::task_period::task_period(boost::posix_time::time_duration, std::function<void()>)’:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:12:91: error: no matching function for call to ‘schelduler::task_timepoint::task_timepoint()’
task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:12:91: note: candidates are:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:6:1: note: schelduler::task_timepoint::task_timepoint(boost::posix_time::ptime, std::function<void()>)
task_timepoint::task_timepoint(ptime run_time, std::function<void(void)> task_function)
^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:6:1: note: candidate expects 2 arguments, 0 provided
In file included from /home/dm3ch/Workspace/Refregiration_Telemetry/Device/src/scheduler.cpp:1:0:
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: schelduler::task_timepoint::task_timepoint(const schelduler::task_timepoint&)
class task_timepoint{
^
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: candidate expects 1 argument, 0 provided
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: schelduler::task_timepoint::task_timepoint(schelduler::task_timepoint&&)
/home/dm3ch/Workspace/Refregiration_Telemetry/Device/include/scheduler.hpp:12:11: note: candidate expects 1 argument, 0 provided
make[2]: *** [CMakeFiles/Refregiration_Telemetry-Device.dir/src/scheduler.cpp.o] Error 1
make[1]: *** [CMakeFiles/Refregiration_Telemetry-Device.dir/all] Error 2
make: *** [all] Error 2
Sorry for my bad English
Your task_period constructor tries to call the default constructor for the task_timepoint base class. You need to call it explicitly like this:
task_period::task_period(time_duration run_period, std::function<void(void)> task_function)
: task_timepoint{run_period, task_function},
run_period{run_period}
{}