1

I'm experiencing a problem at the moment where apparently I am Attempting to reference a deleted function. As far as I can see, I'm not actually referencing a function but a smart pointer to a struct.

This is a university project whereupon multiple header files and CPP files are being used to allow us to understand how to use multiple files in the same project and link them together along with understanding and making use of polymorphism. We are using multiple files as the brief states we must. The files and definitions were provided for us.

The following is supposed to conduct a "Breadth-first" search on a terrain map (array of numbers ranging from 0-3) from a starting location to the goal location. It is about path-finding.

This is what I have so far:

#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; } } 

It's highlighting this line: currentNode->x = openList.front()->x; as the problem.

The NodeList type is defined in SearchBreadthfirst.h as the following:

using NodeList = deque<unique_ptr<SNode>>;

SNode is also defined in SearchBreadthfirst.h as such:

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 }; 

The program breaks upon build. I've been trying to wrap my head around this for days now, so any help is greatly appreciated. If I've missed anything out, let me know and I'll add it in!

James

5
  • 1
    For a start, CSearchBreadthFirst, TerrainMap, and unique_ptr are undefined. Commented Jan 19, 2019 at 21:06
  • Apologies, they are defined in another header file. My bad Commented Jan 19, 2019 at 21:11
  • Why do you have multiple files? Please prepare a minimal reproducible example. Commented Jan 19, 2019 at 21:13
  • @melpomene Apologies. I have updated the question. It is a university project and these files were given to us to utilise. It is part of the brief that we use them. Commented Jan 19, 2019 at 21:18
  • First epistolary SO question I read. A real pleasure. Commented Jan 19, 2019 at 22:22

1 Answer 1

3

The error message Attempting to reference a deleted function is due to the fact that std::unique_ptr explicitly deletes its copy constructor because, obviously, there's only supposed to be one copy of the pointer it contains.

When you call

openList.push_front(start); 

You're creating a copy of start which is of type unique_ptr<SNode> and it has a deleted copy constructor. In order to use a std::unique_ptr with a container, you need to move the object into the container. You need to do something like this:

openList.push_front(move(start)); 

That will move start into the deque and move what was in there into start.

Sign up to request clarification or add additional context in comments.

7 Comments

I had previously tried this, the same error still occurs
I just compiled it locally and that fixed the error for me. I do get a different error though, on the cout line. That's because there's no operator<<() for the SNode type. You need to either write one or print it out a different way, such as manually printing each field.
Strange. I actually used move() initially but because it didn't work I tried it without. The error on the cout line I understand and I'll get to that once this bit works. Very odd though
It seems that passing a unique_ptr by value to the function would generate the same error, would it not?
Yes, it would. You need to send in a const unique_ptr<foo>& instead. (Or non-const if you're going to modify it, I suppose.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.