-3

i have a c2280 error in c++ and i don't know how to solve it. here is the code:

#include <iostream>
#include <queue>
#include <deque>
#include "State.h"
#include <assert.h>

#define MAXIMUM_NUMBER_OF_STATES 1000
#define DELTA_Q 0.1

using namespace std;
class RRT
{

private:

    float inc_dist;
    State start;
    State goal;
    deque<State> states = deque<State>();
    bool goal_is_reached = false;

    float RRT::random(float min, float max){
        // check if min is less than max , if not then exception is thrown
        assert(max >= min);

        float range = max - min;
        float random;

        random = rand() / RAND_MAX;
        return (random * (range)) + min;
    }

    State RRT::randomState(State current_state){

        State state = State();
        srand(time(0));
        while (inStates(state))
        {

            state.x = random(min(current_state.x, goal.x), max(current_state.x, goal.x));
            state.y = random(min(current_state.y, goal.y), max(current_state.y, goal.y));
            state.z = random(min(current_state.z, goal.z), max(current_state.z, goal.z));
        }
        return state;
    }

    bool RRT::inStates(State state){
        for (int i = 0; i < states.size(); i++){
            if (states.at(i).x == state.x && states.at(i).y == state.y && states.at(i).z==state.z)
                return true;
        }
        return false;
    }

    bool RRT::goalTest(State state){
        if (state.x == goal.x && state.y == goal.y && state.z == goal.z){
            return true;
        }
        else
            return false;
    }

    void RRT::Successor(State state){
        State temp3;
        State temp2;
        cout <<endl<< "was"<<endl;
        if (goalTest(state) || states.size() == MAXIMUM_NUMBER_OF_STATES){
            return;
        }

        getNearistNeighbor(temp3=randomState(state));

        cout << "random x: " << temp3.x << " random y: " << temp3.y << " random z: " << temp3.z << endl;
        temp2 = State(temp3);
        temp2.setFather(state);
        states.push_back(temp2);
        states.back().setFather(state);
        Successor(states.back());

        return;

    }

    State RRT::getNearistNeighbor(State sub_goal_state){

        float min_dist = 0, temp;
        State desired_state;

        for (int i = 0; i < states.size(); i++){

            temp = distanceBetweenTwoStates(states.at(i), sub_goal_state);
            if (temp < min_dist){
                min_dist = temp;
                desired_state = State(states.at(i));
            }
        }

        return desired_state;
    }

    void RRT::generatePath(State state){
        cout << endl << "1" << endl;
        if (!state.checkIfNull()){
            cout << endl << "end" << endl;
            return;
            cout << endl << "2" << endl;

            generatePath(*state.getFather().get());
            path.push_back(state);
        }

        cout << endl << "2" << endl;

        generatePath(*state.getFather().get());
        path.push_back(state);

        return;
    }

    float RRT::distanceBetweenTwoStates(State state1, State state2){

        float x_distance, y_distance, z_distance;

        x_distance = sqrt(pow((state1.x - state2.x), 2));
        y_distance = sqrt(pow((state1.y - state2.y), 2));
        z_distance = sqrt(pow((state1.z - state2.z), 2));
        return x_distance + y_distance + z_distance;
    }


public:

    deque<State> path = deque<State>();
    deque<float*> xyzs = deque<float*>();
    RRT::RRT(){
    }

    RRT::RRT(float* starting_point, float* goal_point)
    {
        start = State(starting_point);
        goal= State(goal_point);
        states.push_back(start);
    }

    deque<float*> RRT::getPath(){
        Successor(start);
        for (int i = 0; i < states.size();i++)
        {
            if (goalTest(states.at(i))){
                goal_is_reached = true;
                path.push_back(states.at(i));
                break;
            }
        }
        if (goal_is_reached==false){
            path.push_back(getNearistNeighbor(goal));
        }
        State state;
        state=State(path.at(0));
        path.pop_front();

        cout << endl << "x: " << state.x;
        cout << endl << "y: " << state.y;
        cout << endl << "z: " << state.z << endl;

        generatePath(state);

        convertToXYZ();
        return xyzs;
    }   

    void convertToXYZ(){
        State temp;
        float* xyz = new float[3];

        for (int i = 0; i < path.size(); i++){
            temp = path.at(i);
            xyz[0] = temp.x;
            xyz[1] = temp.y;
            xyz[3] = temp.z;
            xyzs.push_back(xyz);
        }
    }
};

here is the code for State.h:

#include <memory>

using namespace std;

class State
{

private:
    unique_ptr<State> father;
public:

    float x;
    float y;
    float z;

    State(float *xyz){
        x = 0;
        y = 0;
        z = 0;
        father = NULL;
    }

    State(){
        x = 0;
        y = 0;
        z = 0;
        father = NULL;
    }

    State(State& state) : father(new State(*state.father)), x(state.x), y(state.y), z(state.z) {}

    State(unique_ptr<State>& state) : father(new State(*state.get())){}
    void setFather(State& state){
        father.reset(new State(state));
    }

    unique_ptr<State> getFather(){
        unique_ptr<State> temp(new State(*this));
        return temp;
    }

    bool checkIfNull(){
        if (father){
            return true;
        }
        else
            return false;
    }
};

i have been trying to solve this problem but i have not succeed so i need your help guys please help me in solving this. thanks in advance.

here is the error:

Error   8   error C2280: 'std::unique_ptr<State,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function   c:\users\userr\documents\visual studio 2013\projects\devo controller\devo controller\rrt.h  186 1   Devo Controller

again thanks in advance.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
user3677513
  • 21
  • 1
  • 4
  • 1
    You should post the exact error message but also clean the code to isolate the problem. You may also take some more time to solve the problem yourself... – jpo38 Mar 08 '15 at 11:28
  • 1
    Several things would help: 1. Remove extra code not needed to reproduce the problem. 2. Post which line the error(s) occur on. 3. Post the full error message, not just the code-name (not everyone uses Microsoft compilers, so would have to google what c2208 means) – Mats Petersson Mar 08 '15 at 11:32
  • 1
    It ALSO helps tremendously to have the correct error code, and not swap two digits in it, as that has a different meaning. – Mats Petersson Mar 08 '15 at 11:42
  • `State(State& state) : father(new State(*state.father)),` - looks like it should be `*state.father.get()` in there – M.M Mar 08 '15 at 12:14
  • I suspect the problem is that you have not defined `State`'s assignment operator, and the error message comes from the compiler's attempt to generate a default assignment operator. Try either implementing State's assignment operator or declaring it as `delete`d and see if the error goes away. – M.M Mar 08 '15 at 12:15
  • 1
    BTW this seems like a bad way of using `unique_ptr`. That's designed for ownership semantics but in your classes the `unique_ptr` instances do not have ownership semantics. It seems like a total mess TBH. Perhaps using `shared_ptr` and `weak_ptr` would be better, or maybe even raw pointers. – M.M Mar 08 '15 at 12:17

1 Answers1

2
   unique_ptr<State> getFather(){
        unique_ptr<State> temp(new State(*this));
        return temp;
    }

you can't return unique_ptr . returning a value from function means using the copy constructor. the copy constructor is disabled for unique_ptr. why? because you can't copy a unique_ptr , it's unique! you need to use shared_ptr if many pointers are to point to a specific object. the same goes for the assignment operator ( = ) .

PS. I think it's deprecated to explicitly assign object memory addres to unique_ptr (with new) , the new standard forces you to use std::make_unique in order to assign something to unique_ptr. also, try googling your error first, you'll be surprised how many answers there are out there

David Haim
  • 25,446
  • 3
  • 44
  • 78
  • I don't think this is right: returning from function is a copy/move so it is permitted if the class has a move constructor. In fact `std::make_unique` returns `unique_ptr` by value. – M.M Mar 08 '15 at 12:22