尝试引用已删除的函数(不引用函数)

Attempting to reference a deleted function (No reference to a funciton made)

本文关键字:函数 引用 删除      更新时间:2023-10-16

我现在遇到了一个问题,显然我正在Attempting to reference a deleted function.据我所知,我实际上并不是在引用函数,而是指向结构的智能指针。

这是一个大学项目,其中使用多个头文件和CPP文件,使我们能够了解如何在同一项目中使用多个文件,并将它们链接在一起,同时理解和利用多态性。我们使用多个文件作为我们必须的简要状态。为我们提供了文件和定义。

以下内容应该在从起始位置到目标位置的地形图(0-3 的数字数组)上进行"广度优先"搜索。这是关于寻路的。

这是我到目前为止所拥有的:

#include "SearchBreadthfirst.h" // Declaration of this class
#include <iostream>
#include <list>
using namespace std;
bool CSearchBreadthFirst::FindPath(TerrainMap& terrain, unique_ptr<SNode> start, unique_ptr<SNode> goal, NodeList& path)
{
// Initialise Lists
NodeList closedList;    // Closed list of nodes
NodeList openList;      // Open list of nodes
unique_ptr<SNode>currentNode(new SNode);    // Allows the current node to be stored
unique_ptr<SNode>nextNode(new SNode);       // Allows the next nodes to be stored in the open list
// Boolean Variables
bool goalFound = false; // Returns true when the goal is found
// Start Search
openList.push_front(move(start)); // Push the start node onto the open list
// If there is data in the open list and the goal hasn't ben found
while (!openList.empty() || goalFound == false)
{
cout << endl << "Open list front:" << openList.front() << endl;
currentNode->x = openList.front()->x;
currentNode->y = openList.front()->y;
currentNode->score = openList.front()->score;
currentNode->parent = openList.front()->parent;
}
}

它突出了这句话:currentNode->x = openList.front()->x;是问题所在。

NodeList类型在SearchBreadthfirst.h中定义如下:

using NodeList = deque<unique_ptr<SNode>>;

SNodeSearchBreadthfirst.h中也有这样的定义:

struct SNode
{
int x;             // x coordinate
int y;             // y coordinate
int score;         // used in more complex algorithms
SNode* parent = 0; // note use of raw pointer here
};

程序在构建时中断。几天来我一直在尝试解决这个问题,所以任何帮助都非常感谢。如果我遗漏了什么,请告诉我,我会添加它!

詹姆斯

错误消息Attempting to reference a deleted function是由于std::unique_ptr显式delete其复制构造函数,因为显然,它包含的指针只有一个副本。

当您致电时

openList.push_front(start);

您正在创建类型为unique_ptr<SNode>start的副本,并且它有一个已删除的复制构造函数。为了将std::unique_ptr与容器一起使用,您需要将对象移动到容器中。你需要做这样的事情:

openList.push_front(move(start));

这会将start移动到deque中,并将其中的内容移动到start中。