未定义的引用错误对我来说没有任何意义

Undefined reference error doesn't make any sense to me

本文关键字:任何意 对我来说 引用 错误 未定义      更新时间:2023-10-16

我这里有这段代码:
主.cpp

#include "AStarPlanner.h"
#include <costmap_2d/costmap_2d.h>
int main(int argc, char** argv)
{
   AStarPlanner planner =  AStarPlanner(10,10,&costmap);
}

和我的班级:
AStarPlanner.h

class AStarPlanner {
public:
  AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap);
  virtual ~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;
}

我看不出我有什么错误。函数已定义,我的主.cpp知道该类。

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)

但是我收到此错误:
vtable for AStarPlanner'** ** undefined reference to AStarPlanner::~AStarPlanner((' 的未定义引用

您未能为 AStarPlanner 定义析构函数。您可以将其添加到 AStarPlanner 中.cpp从而:

AStarPlanner::~AStarPlanner()
{
}

考虑这个建议。

我也遇到了未定义引用的问题,大多数时候这表明存在链接问题......

我解决了:

类似于Lee Netherton写的,但直接将其添加到您的CmakeLists中.txt

确保添加失败函数的实现所在的文件。

在您的示例中,请确保:

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

这将告诉链接器要针对您的robot_mover可执行文件查找所有定义/实现的源文件......

该错误意味着尽管编译器可以找到main()类的定义,但链接器无法找到。 您需要设置编译选项,以便 yopu 在尝试构建可执行文件时将生成的AStarPlanner.obj传递给链接器

如何进行设置的确切形式取决于您使用的编译器。

有了gcc,它应该编译如下:

gcc -o main Main.cpp AStarPlanner.cpp

我的猜测是你错过了AStarPlanner.cpp部分。

编辑:

休?您收到的错误刚刚在OP中更改。这个答案现在没有多大意义。

编辑2:

看起来您正在将AStarLibrary放入robot_mover库中。在构建可执行文件时是否链接到此库?我不熟悉ros*宏,但在普通的 gcc 中,构建命令如下所示:

gcc -o main Main.cpp -lrobot_mover

AStarPlanner.cpp可能没有被编译/链接。确保它在项目中。

您的

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

引用成本图,但我没有看到它的定义(针对变量本身,而不是针对类(。