# Why does my compiler warn at usage of operators of ros::TimeBase?

When I use ros::Time::operator<(ros::Time) like this:

ros::Time start=ros::Time::now();
// Some Processing
ros::Time end=ros::Time::now();
if(start>end) {} // Just an example...


I always get compiler warnings like

Warning: instantiation of function 'ros::TimeBase<ros::time, ros::duration="">::operator<' required here, but no definition is available. Add an explicit instantiation declaration to suppress this warning if 'ros::TimeBase<ros::time, ros::duration="">::operator<' is explicitly instantiated in another translation unit

Same thing happens for other operators like + or -. I'm not very experienced with template programming and I don't really know, what these warnings are supposed to tell me.
Now my questions are:
1) Am I using the operators wrong?
2) If not: Is it safe to use them this way?
3) If so: Would it be fine to just add those forward declarations to my header files as suggested by the compiler?

EDIT:
As requested by @Weasfas here a full example:

test_node.cpp:

#include <ros/ros.h>
#include <std_msgs/Bool.h>

int main()
{
ros::Time start(ros::Time::now());
ros::Time end(ros::Time::now());
ROS_INFO("%d",start<end);
}


CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(test_pack)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
sensor_msgs
message_generation
roslib
)
include_directories(
../../devel/include
${catkin_INCLUDE_DIRS} ) add_executable(test_node src/test_node.cpp) target_link_libraries(test_node${catkin_LIBRARIES})


This code will trigger a warning as described before.

edit retag close merge delete

Hi,

The problem is not about using it properly or not. The class ros::Time does not provide the ros::Time operators like <, >, +, -, * ...

You may want to use ros::Duration instead of ros::Time because ros::Time refers to a specific moment in time whereas ros::Duration refers to a period of time. Take a look at the ros::Timewiki.

( 2019-12-10 09:17:56 -0600 )edit

@Weasfas I already had a look at ros::Time wiki. If you look under 1.4 line 3 you will see a call to ros::Time::operator+. Also looking at the API, that is linked in the wiki, you will see, that ros::Time indeed does have all those operators. The operators are inherited from ros::TimeBase.

( 2019-12-10 09:25:25 -0600 )edit

@max11gen Are you sure you have the cmake and headers properly set up?. I am using a similar example and all is compiled and executed perfectly:

    ros::Time start=ros::Time::now();
ros::Duration(2.0).sleep();
ros::Time end=ros::Time::now();

if(end > start)
{
ROS_INFO("Secs: %d ---- %d", start.sec, end.sec);
}

( 2019-12-10 09:30:35 -0600 )edit

@Weasfas I think you missunderstood me. My code doesn't fail to compile, it compiles fine. I just get warnings (no errors) and I always try to keep my code free of warnings. Thanks for your efforts though.

( 2019-12-10 09:39:39 -0600 )edit

@max11gen Ok, lets put it this way. I compiled your code, my code and I got no warnings, so it is something related to your set up. Tested in both: Kinetic and Melodic.

( 2019-12-10 09:42:04 -0600 )edit

@Weasfas Ah ok. Well, I really have no idea where to search for problems in my setup, when actually everything compiles...

( 2019-12-10 10:24:57 -0600 )edit

@max11gen Yes, that is a problem. Because I think that in your case the warning is important since you are using the output of the operator in a conditional clause and you have no clue of what the if is evaluating. Have you tried to use the CMakeList provided by the default catkin package generator? or even the ones provided in the roscpp tutorials. This is the only thing I come up with in order to troubleshoot that problem.

( 2019-12-10 10:42:48 -0600 )edit

@Weasfas I build all my packages starting with the CMakeList provided by default catkin package generator. Then I iteratively comment in, whatever I need and extend the CMakeList. Building the package with the plain default is not an option now, since it doesn't set all dependencies that my packaged needs to be compiled.

( 2019-12-11 03:40:30 -0600 )edit