未定义的引用错误对我来说没有任何意义
Undefined reference error doesn't make any sense to me
我这里有这段代码:
主.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);
引用成本图,但我没有看到它的定义(针对变量本身,而不是针对类(。
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 对自定义元素向量进行排序时出现意外(至少对我来说)行为
- 这对我来说真的很难,我该怎么办
- mod 功能对我来说无法正常工作
- 在这种情况下,为什么Sfinae对我来说不正确以及如何修复它
- 使用嵌套环以简单(对我来说很难)输出
- 是否可以安全地假设64位指针中的16个高位(2个对我来说足够了)是未设置的
- 无符号和有符号int(对我来说)之间的比较似乎是必需的(c++)
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- libc ++is_copy_constructible对我来说似乎是错误的
- 对我来说,使 boost::statechart::state_machine 线程安全的最简单方法是什么?
- 未定义的引用错误对我来说没有任何意义
- 类函数在工作时被击中是错过的,即使它们是相同的(对我来说)
- default_random_engine对我来说不是随机值
- 提升异步操作不起作用(对我来说)
- SEG在非常复杂的(对我来说)程序中出错
- 为什么我在Codechef的褪色回文中得到一个WA,即使我在我的代码中没有发现任何错误,它对我来说工作得很好
- 洛基的长寿功能对我来说似乎不安全,是这样吗?
- Q设置对我来说效果不佳
- CUDAMemcpy对我来说毫无意义…为什么要在普通c++中指定设备内存?