我现在遇到了一个问题,显然我是 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>>;
SNode
也在 SearchBreadthfirst.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
s 是它的复制构造函数,因为很明显,它所包含的指针应该只有一个拷贝。
当你打电话时
openList.push_front(start);
您正在创建 start
的拷贝这是类型 unique_ptr<SNode>
它有一个删除的复制构造函数。为了使用 std::unique_ptr
使用容器,您需要将对象移动到容器中。你需要做这样的事情:
openList.push_front(move(start));
那会移动 start
进入deque
把里面的东西移到start
.
关于c++ - 试图引用已删除的函数(未引用已创建的函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271376/