我改变了我的列表(和代码),所以它现在处理Node*而不是Node Objects,这导致了一个未定义的引用错误

c++ i change my list(and code) so it now handles Node* rather than Node Objects and it has caused an undefined reference error

本文关键字:Node Objects 错误 未定义 引用 一个 处理 代码 列表 我的 改变      更新时间:2023-10-16

我试图在c++中做一个a *,但我有一个解决方案后的路径问题。我的老师建议我使用Node*并使用new在堆上创建它们,因为我的旧的是在堆栈上分配它们,显然你不应该在堆栈上返回指针,所以它在运行时弄乱了代码。

我遇到的问题是,当我改变我的列表保持,而不是我得到这个错误:

undefined reference to `checkInClosedList(std::vector<Node*, std::allocator<Node*> >, int, int)'
undefined reference to `placeInOpen(std::list<Node*, std::allocator<Node*> >, int, int)'

当我调用这些函数时:

if(placeInOpen(openList,best->getX()-1, best->getY())){
      //do something
}
if(!checkInClosedList(closedList, best->getX(), best->getY())){
    // do something
}

给出best和openList的代码:

list<Node*> openList;
vector<Node*> closedList;
Node *end;
Node *start = initiateStart(map);
openList.push_front(start);
while (!openList.empty()) {
    Node *best = openList.front();
    openList.pop_front();
   }
   //some other logic.....

函数在头文件中定义如下:

bool placeInOpen(std::list<Node*>& v,int x, int y);
bool checkInClosedList(std::vector<Node*>& v, int x, int y);

在我编辑我的函数和列表以包含Node*这工作得很好,为什么它对指针不同(我已经正确处理了指针操作,只是这个错误导致我头痛)

欢呼,克里斯。

您已经将函数声明更改为新类型,但是您还必须更改定义

然而,我建议你根本不要做这个更改;你的老师似乎在胡说八道。Node对象在列表中,而不是"在堆栈上",它们的生命周期由列表正确管理。存储动态对象的指针会导致内存泄漏,甚至更糟。

bool checkInClosedList(std::vector<Node*>& v, int x, int y);只是一个前向声明。它告诉编译器在其他地方期望具有该签名的函数的工作实现。你必须在你的函数实现中改变它