ome/bobac3/rosworkspace/src/myplanner/src/myplanner.cpp: In destructor ‘virtual…
- User
ome/bobac3/ros_workspace/src/my_planner/src/my_planner.cpp: In destructor ‘virtual my_planner::MyPlanner::~MyPlanner()’: /home/bobac3/ros_workspace/src/my_planner/src/my_planner.cpp:39:12: error: ‘tf_listener_’ was not declared in this scope if(tf_listener_) { ^~~~~~~~~~~~ /home/bobac3/ros_workspace/src/my_planner/src/my_planner.cpp:40:20: error: type ‘’ argument given to ‘delete’, expected pointer delete tf_listener_; ^~~~~~~~~~~~ my_planner/CMakeFiles/my_planner.dir/build.make:62: recipe for target 'my_planner/CMakeFiles/my_planner.dir/src/my_planner.cpp.o' failed make[2]: *** [my_planner/CMakeFiles/my_planner.dir/src/my_planner.cpp.o] Error 1 CMakeFiles/Makefile2:8473: recipe for target 'my_planner/CMakeFiles/my_planner.dir/all' failed make[1]: *** [my_planner/CMakeFiles/my_planner.dir/all] Error 2 Makefile:140: recipe for target 'all' failed make: *** [all] Error 2
- Assistant
You are getting the following errors in your C++ ROS package:
/home/bobac3/ros_workspace/src/my_planner/src/my_planner.cpp:39:12: error: ‘tf_listener_’ was not declared in this scope if(tf_listener_) { ^~~~~~~~~~~~ /home/bobac3/ros_workspace/src/my_planner/src/my_planner.cpp:40:20: error: type ‘<type error>’ argument given to ‘delete’, expected pointer delete tf_listener_; ^~~~~~~~~~~~What do these errors mean?
-
‘tf_listener_’ was not declared in this scope
The compiler cannot find a variable namedtf_listener_in the scope of your destructor. -
type ‘’ argument given to ‘delete’, expected pointer
The compiler is trying to delete something that is not a pointer, or it doesn't know the type oftf_listener_.
How to Fix
1. Check Declaration in Header File
Make sure
tf_listener_is declared as a pointer in your class header file (my_planner.h), for example:#include <tf/transform_listener.h> namespace my_planner { class MyPlanner { public: // ... virtual ~MyPlanner(); private: tf::TransformListener* tf_listener_; // <-- Make sure this is a pointer }; }2. Check Initialization in Constructor
Make sure you initialize
tf_listener_in your constructor, e.g.:MyPlanner::MyPlanner() { tf_listener_ = new tf::TransformListener(); }3. Destructor Implementation
Your destructor should look like:
MyPlanner::~MyPlanner() { if (tf_listener_) { delete tf_listener_; tf_listener_ = nullptr; } }4. If Using Smart Pointers
If you are using
std::unique_ptrorstd::shared_ptr, you do not need to manually delete. For example:std::unique_ptr<tf::TransformListener> tf_listener_;and in the constructor:
tf_listener_ = std::make_unique<tf::TransformListener>();and in the destructor, do not delete it manually.
Summary Checklist
- Is
tf_listener_declared as a pointer in your header file? - Is it initialized in your constructor?
- Are you deleting it only if it is a pointer?
- If using smart pointers, do not manually delete.
Example: my_planner.h
#include <tf/transform_listener.h> namespace my_planner { class MyPlanner { public: MyPlanner(); virtual ~MyPlanner(); private: tf::TransformListener* tf_listener_; }; }Example: my_planner.cpp
#include "my_planner/my_planner.h" namespace my_planner { MyPlanner::MyPlanner() { tf_listener_ = new tf::TransformListener(); } MyPlanner::~MyPlanner() { if (tf_listener_) { delete tf_listener_; tf_listener_ = nullptr; } } }
If you still have issues, please paste your class declaration and destructor code for more specific help!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.