Thinking about intelligent pointer in C++

2 min read
It returned a build error when I was implementing the creation of a ROS2 node (yes I'm a beginner of ROS2):
In file included from /usr/include/c++/13/bits/stl_tempbuf.h:61,
from /usr/include/c++/13/memory:66,
from /opt/ros/jazzy/include/rclcpp/rclcpp/rclcpp.hpp:169,
from /home/hengjia/Workspace/ros2_ws/src/my_cpp_pkg/src/my_first_node.cpp:2:
/usr/include/c++/13/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_Tp*, _Args&& ...) [with _Tp = rclcpp::Node; _Args = {}]’:
/usr/include/c++/13/bits/alloc_traits.h:661:19: required from ‘static void std::allocator_traits<std::allocator<void> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = rclcpp::Node; _Args = {}; allocator_type = std::allocator<void>]’
/usr/include/c++/13/bits/shared_ptr_base.h:604:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {}; _Tp = rclcpp::Node; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/13/bits/shared_ptr_base.h:971:16: required from ‘std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = rclcpp::Node; _Alloc = std::allocator<void>; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/13/bits/shared_ptr_base.h:1712:14: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {}; _Tp = rclcpp::Node; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/13/bits/shared_ptr.h:464:59: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {}; _Tp = rclcpp::Node]’
/usr/include/c++/13/bits/shared_ptr.h:1009:14: required from ‘std::shared_ptr<typename std::enable_if<(! std::is_array< <template-parameter-1-1> >::value), _Tp>::type> std::make_shared(_Args&& ...) [with _Tp = rclcpp::Node; _Args = {}; typename enable_if<(! is_array< <template-parameter-1-1> >::value), _Tp>::type = rclcpp::Node]’
/home/hengjia/Workspace/ros2_ws/src/my_cpp_pkg/src/my_first_node.cpp:37:45: required from here
/usr/include/c++/13/bits/stl_construct.h:119:7: error: no matching function for call to ‘rclcpp::Node::Node()’
119 | ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/ros/jazzy/include/rclcpp/rclcpp/executors/single_threaded_executor.hpp:28,
from /opt/ros/jazzy/include/rclcpp/rclcpp/executors.hpp:22,
from /opt/ros/jazzy/include/rclcpp/rclcpp/rclcpp.hpp:172:
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:1629:3: note: candidate: ‘rclcpp::Node::Node(const rclcpp::Node&, const std::string&)’
1629 | Node(
| ^~~~
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:1629:3: note: candidate expects 2 arguments, 0 provided
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:103:12: note: candidate: ‘rclcpp::Node::Node(const std::string&, const std::string&, const rclcpp::NodeOptions&)’
103 | explicit Node(
| ^~~~
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:103:12: note: candidate expects 3 arguments, 0 provided
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:91:12: note: candidate: ‘rclcpp::Node::Node(const std::string&, const rclcpp::NodeOptions&)’
91 | explicit Node(
| ^~~~
/opt/ros/jazzy/include/rclcpp/rclcpp/node.hpp:91:12: note: candidate expects 2 arguments, 0 provided
gmake[2]: *** [CMakeFiles/cpp_node.dir/build.make:76: CMakeFiles/cpp_node.dir/src/my_first_node.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:137: CMakeFiles/cpp_node.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2
---
Failed <<< my_cpp_pkg [1.28s, exited with code 2]
Summary: 0 packages finished [1.36s]
1 package failed: my_cpp_pkg
1 package had stderr output: my_cpp_pkg
Here is my code:
class MyNode : public rclcpp::Node
{
public:
MyNode()
: Node("cpp_test")
{
RCLCPP_INFO(this->get_logger(), "Hello, ROS 2!");
// Create a timer that calls the timer_callback function every second
timer_ = this->create_wall_timer(
std::chrono::seconds(1),
std::bind(&MyNode::timer_callback, this));
}
void timer_callback()
{
RCLCPP_INFO(this->get_logger(), "Timer callback triggered. ");
}
private:
rclcpp::TimerBase::SharedPtr timer_;
// Add any other member variables or methods as needed
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>();
// RCLCPP_INFO(node->get_logger(), "Hello, ROS 2!");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
I just fixed this error by changing the code:
auto node = std::make_shared<MyNode>();
Then it worked:
hengjia@hengjia-Alienware-m18-R2:~/Workspace/ros2_ws$ colcon build --packages-select my_cpp_pkg
Starting >>> my_cpp_pkg
Finished <<< my_cpp_pkg [1.61s]
Summary: 1 package finished [1.68s]
0
Subscribe to my newsletter
Read articles from Hengjia Xiao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
