Thread: Error when creating a subclass object

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    Error when creating a subclass object

    Hi,

    I am relatively beginner in C++. I have the following question.
    I have two classes, CMotionPlanner which is the base class, and CCollisionObject which is the derived / subclass.

    Code:
    class CCollisionObject : public CMotionPlanner
    {
    public:
      CCollisionObject() : CMotionPlanner()
      {
         m_pubObstaclesPub = m_nh.advertise<moveit_msgs::CollisionObject>("/collision_object", 10);
      }
    
      void AddObstacle(int choice);
    
    private:
      ros::Publisher m_pubObstaclesPub;
      moveit_msgs::CollisionObject          m_CollisionObject;
      shape_msgs::SolidPrimitive primitive;
    
    };
    Then, I create an object of the subclass, so that I can invoke class methods. See the code below.

    Code:
    int main(int argc, char* argv[])
    {
      ros::init(argc, argv, "ccollisiobobject");
    
      CCollisionObject co;
    
      int selection = atoi(argv[1]);
      co.AddObstacle(selection);
    
      ros::shutdown();
    
      return 0;
    }
    However, I get errors. The program does not compile. The error is the following.
    Code:
    CMakeFiles/obs_node.dir/src/ccollisionobject.cpp.o: In function `CCollisionObject::CCollisionObject()':
    ccollisionobject.cpp:(.text._ZN16CCollisionObjectC2Ev[_ZN16CCollisionObjectC5Ev]+0x24): undefined reference to `CMotionPlanner::CMotionPlanner()'
    collect2: error: ld returned 1 exit status
    The base class constructor looks like this.
    Code:
    CMotionPlanner::CMotionPlanner() :
      m_strPlanningGroup("manipulator"),
      m_MoveGroup(m_strPlanningGroup)
    {
      m_SamplePose.orientation.w = 0.0;
      m_SamplePose.position.x = 0.51;
      m_SamplePose.position.y = 0.3;
      m_SamplePose.position.z = 0.8;
    }


    Please, can you help me with this.

    thanks,
    Zahid
    Last edited by zahid990170; 09-15-2021 at 05:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So do you have a cmotionplanner.cpp file?

    Are you compiling it?

    > undefined reference to
    These typically result from
    - not compiling all your source code
    - not being able to spell correctly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    thanks @salem for pointer in this direction.

    I have a CMakeLists.txt file. I wanted to create separate ROS nodes (executable) one based on CMotionPlanner and other based on CCollisionObject. Thus, initially, my CMakeLists.txt looked as follows.

    Code:
    add_executable(motion_node src/main.cpp src/motionplanner.cpp)
    add_executable(obs_node src/ccollisionobject.cpp)
    
    ## Specify libraries to link a library or executable target against
    target_link_libraries(motion_node ${catkin_LIBRARIES})
    target_link_libraries(obs_node ${catkin_LIBRARIES})
    but, when I changed it to
    Code:
    add_executable(motion_node src/main.cpp src/motionplanner.cpp)
    add_executable(obs_node src/motionplanner.cpp src/ccollisionobject.cpp)
    
    ## Specify libraries to link a library or executable target against
    target_link_libraries(motion_node ${catkin_LIBRARIES})
    target_link_libraries(obs_node ${catkin_LIBRARIES})
    it compiles correctly, producing two nodes motion_node, and obs_node.

    Do you think, this kind of reasoning has any logical flaws to it.

    thanks,
    Zahid

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. object not creating????
    By shansajid in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2013, 06:45 AM
  2. Creating moving object. is any way to do it in other way?
    By Grouw Patter in forum C++ Programming
    Replies: 12
    Last Post: 10-27-2012, 10:12 AM
  3. creating object with [new] ?
    By wolfindark in forum C++ Programming
    Replies: 11
    Last Post: 05-13-2007, 12:31 PM
  4. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  5. Creating a new object of a subclass problems.
    By Goombaz in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2006, 08:33 AM

Tags for this Thread