ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Undefined reference to parent NON default constructor

asked 2014-12-24 23:42:35 -0600

RSA_kustar gravatar image

updated 2014-12-24 23:53:31 -0600

I have the parent class

//parent.h 
class parent 
{
parent(){std::cout << "default parent constructor" << std::endl;} ; 
parent(ros::Nodehandel n_) ;
~parent (){} ; 
protected :
ros::NodeHandle n ; 
}

// parent.cpp 
parent::parent(ros::NodeHandel n_): n(n_)
{
std::cout << " parent constructor" << std::endl;
publishers
Subscriber 
}

/////////////////////////////////////////////

 // child.cpp 
class child : public parent 
{
child (){std::cout << "default child constructor" << std::endl ; } ; 
child (ros::NodeHandle n){std::cout << "child constructor" << std::endl ; }  
 //   child (ros::NodeHandle n):: parent (n) {std::cout << "child constructor" << std::endl ; }  // NOT WORKING THERE IS AN ERROR
~child () ; 
}

/////////////

  int main ( .... ) 
   {
  ros::init(argc, argv, "node");
  ros::NodeHandle n;
  ros::NodeHandle n_priv("~");
double freq;

n_priv.param<double>("frequency", freq, 10.0);
ros::Rate loop_rate(freq);

child prf_obj(n);

while(ros::ok())
{
    std::cout << "loop" << std::endl;

    ros::spinOnce();
    loop_rate.sleep();
}
return 0;

 }

For the following code I get the following results:

default parent constructor
child constructor
loop
loop
.
.
.

/////

What I want to is to call the non default constructor for the parent where I define the publisher and subscriber ?? but When I do the following:

  child (ros::NodeHandle n):: parent (n) {std::cout << "child constructor" << std::endl ; }  // NOT WORKING THERE IS AN ERROR

The ERROR: undefined reference to `parent::parent(ros::NodeHandle&)

I added the parent.h filed in include folder in the package. the parent.cpp and child.cpp are in the src folder in the package. I also added in the Cmakelist

 include_directories(include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
  )

 add_library(controller
 src/parent.cpp
  )

add_executable(run_node src/child.cpp)
add_dependencies(run_node ${PROJECT_NAME}_gencfg ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(run_node
 ${catkin_LIBRARIES}
 ${Boost_LIBRARIES}
  )

is there any thing I have to add any where else to fix this problem ???

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-12-29 03:44:32 -0600

RSA_kustar gravatar image

I solved it by adding the child.cpp file and the parent.cpp in the Cmakelist in the following line:

add_executable(run_node src/child.cpp src/parent.cpp )

edit flag offensive delete link more
0

answered 2014-12-25 05:01:35 -0600

bvbdort gravatar image

To call parent constructor from derived class use syntax : not ::

child (ros::NodeHandle n) : parent (n) {std::cout << "child constructor" << std::endl ; }
edit flag offensive delete link more

Comments

I noticed this error but that was not the error I was talking about it

RSA_kustar gravatar image RSA_kustar  ( 2014-12-29 03:43:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-12-24 23:42:35 -0600

Seen: 656 times

Last updated: Dec 29 '14