2

I have this code here:
Main.cpp

#include "AStarPlanner.h"
#include <costmap_2d/costmap_2d.h>

int main(int argc, char** argv)
{
   AStarPlanner planner =  AStarPlanner(10,10,&costmap);
}

and my class:
AStarPlanner.h

class AStarPlanner {

public:

  AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap);
  virtual ~AStarPlanner();

AStarPlanner.cpp

#include "AStarPlanner.h"

using namespace std;

AStarPlanner::AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap)
{

  ROS_INFO("Planner Konstruktor");
  width_ = width;
  height_ = height;
  costmap_ = costmap;
}

I can't see any mistake from me. The function is defined and my main.cpp knows about the class.

CMakeList

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

rosbuild_add_library (robot_mover src/AStarPlanner.cpp )
rosbuild_add_executable(robot_mover src/main.cpp)

But i get this error :
undefined reference to vtable for AStarPlanner'** ** undefined reference toAStarPlanner::~AStarPlanner()'

madmax
  • 1,803
  • 3
  • 25
  • 50

6 Answers6

4

You have failed to define a destructor for AStarPlanner. You may add it to AStarPlanner.cpp thus:

AStarPlanner::~AStarPlanner()
{
}

Consider this advice.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
2

I had a problem of undefined referenced too, which most of the times indicates for a linking issue...

I solved it:

Similar to what Lee Netherton wrote, but add it directly in your CmakeLists.txt

Make sure to add the file that the implementation of the function that failing is in.

In your example make sure that:

 add_executable(robot_mover 
                src/main.cpp
                AStarPlanner.cpp)

That will tell the linker which source files to look for all definitions/implementations against your robot_mover executable...

Kohn1001
  • 3,507
  • 1
  • 24
  • 26
1

The error means that although the compiler could find the defintion of the class for main(), but the linker could not. You need to set up your compilation options so that yopu pass the generated AStarPlanner.obj to the linker when it tries to build your executable

The exact form of how to do the setup depends on what compiler you are using.

Attila
  • 28,265
  • 3
  • 46
  • 55
1

With gcc it should be compiled with something like:

gcc -o main Main.cpp AStarPlanner.cpp

My guess is that you've missed the AStarPlanner.cpp part.

Edit:

Hugh? The error you're getting just changed in the OP. This answer won't make a lot of sense now.

Edit2:

It looks like you're putting AStarLibrary into a robot_mover library. Are you linking to this library when building your executable? I'm not familiar with the ros* macros, but in ordinary gcc, the build command would look something like this:

gcc -o main Main.cpp -lrobot_mover
Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
  • je sorry, looked in my cmakelist and found an error. So its not solved but the error changed ;) – madmax May 22 '12 at 16:21
  • Well, I think the linking happens in the CMakeList because for compiling I just need to call rosmake. – madmax May 22 '12 at 16:32
  • Yep, @Robᵩ is right! And for completion, the robot_mover name, is a package in the Robotic Operating System. – madmax May 22 '12 at 16:35
0

AStarPlanner.cpp probably isn't being compiled/linked. Make sure it's in the project.

David
  • 27,652
  • 18
  • 89
  • 138
-1

Your

AStarPlanner planner =  AStarPlanner(10,10,&costmap);

references costmap but I don't see a definition for it (for the variable itself, not for the class).

Igor F.
  • 2,649
  • 2
  • 31
  • 39