ROSMath class which contain a class Math ( or a pointer) in order to add two integers

asked 2017-12-28 09:03:29 -0500

Hamza gravatar image

updated 2017-12-28 13:00:46 -0500

jayess gravatar image

Hi,

I'm new to ROS and I'm trying to creating a ROSMath class which contain a class Math ( or a pointer) in order to add two integers. Actually, i dispose 3 .cpp ( I put them in test /src where test = a package that i created files and a 2 .h files (catkin_ws/include/test)

Math.h
#ifndef DEF_MATH
#define DEF_MATH



class Math
{

public:


static int add (int a , int b ) ;

};

#endif

ROSMath.h:

#ifndef DEF_ROSMath
#define DEF_ROSMath

class ROSMath 
{

private: 
Math theMath;

public:

  ROSMath(Math* aMath);

  bool ROSadd(test::TwoInts::Request & req,
           test::TwoInts::Response &res);
};

#endif

Math.cpp:

#include "test/Math.h"



int add(int a, int b)
{

return a+b;

}

ROSMath.cpp:

#include "test/Math.h"
#include "test/ROSMath.h"
#include "ros/ros.h"
#include "test/TwoInts.h"


ROSMath::ROSMath(Math* aMath) {
theMath = aMath;

}

bool ROSMath:: ROSadd(test::TwoInts::Request &req,
                  test::TwoInts::Response &res);
{

res.sum = theMath->add( req.a , req.b);
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("  sending back response: [%ld]", (long int)res.sum);
return true;

}

main.cpp:

#include "ros/ros.h"
#include "test/TwoInts.h"
#include "test/ROSMath.h"
#include "test/Math.h"

int main(int argc, char **argv)
{
  ros::init(argc, argv, "add_two_ints_test_server");
  ros::NodeHandle n;

// %Tag(SERVICE_SERVER)%

  Math op; 
  ROSMath ROSop(&op);
  ros::ServiceServer ss = n.advertiseService("add_two_ints_test", &ROSMath::ROSadd, &ROSop);
// %EndTag(SERVICE_SERVER)%

  ros::spin();

  return 0;
}

I add this lines to my Cmakelists.txt file :

add_executable(main src/main.cpp src/ROSMath.cpp src/Math.cpp)
target_link_libraries(main ${catkin_LIBRARIES})
add_dependencies(main test_gencpp)

But I got too many errors when I try catkin_make :

In file included from /home/hamza/catkin_ws/src/test/src/ROSMath.cpp:2:0:
/home/hamza/catkin_ws/devel/include/test/ROSMath.h:14:15: error: ‘test’ has not been declared
   bool ROSadd(test::TwoInts::Request & req,
               ^
/home/hamza/catkin_ws/devel/include/test/ROSMath.h:14:38: error: expected ‘,’ or ‘...’ before ‘&’ token
   bool ROSadd(test::TwoInts::Request & req,
                                      ^
/home/hamza/catkin_ws/src/test/src/ROSMath.cpp: In constructor ‘ROSMath::ROSMath(Math*)’:
/home/hamza/catkin_ws/src/test/src/ROSMath.cpp:8:9: error: no match for ‘operator=’ (operand types are ‘Math’ and ‘Math*’)
 theMath = aMath;
         ^
In file included from /home/hamza/catkin_ws/src/test/src/ROSMath.cpp:1:0:
/home/hamza/catkin_ws/devel/include/test/Math.h:6:7: note: candidate: Math& Math::operator=(const Math&)
 class Math
       ^
/home/hamza/catkin_ws/devel/include/test/Math.h:6:7: note:   no known conversion for argument 1 from ‘Math*’ to ‘const Math&’
/home/hamza/catkin_ws/src/test/src/ROSMath.cpp: At global scope:
/home/hamza/catkin_ws/src/test/src/ROSMath.cpp:12:6: error: prototype for ‘bool ROSMath::ROSadd(test::TwoInts::Request&, test::TwoInts::Response&)’ does not match any in class ‘ROSMath’
 bool ROSMath:: ROSadd(test::TwoInts::Request &req,
      ^
In file included from /home/hamza/catkin_ws/src/test/src/ROSMath.cpp:2:0:
/home/hamza/catkin_ws/devel/include/test/ROSMath.h:14:8: error: candidate is: bool ROSMath::ROSadd(int)
   bool ROSadd ...
(more)
edit retag flag offensive close merge delete