无法解决错误c2280 C ++

时间:2015-03-08 11:26:15

标签: c++ copy-constructor smart-pointers unique-ptr deep-copy

我在c ++中有一个c2280错误,我不知道如何解决它。 这是代码:

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

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

我一直在努力解决这个问题,但我没有成功,所以我需要你的帮助,请帮助我解决这个问题。 提前谢谢。

这是错误:

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

再次感谢。

1 个答案:

答案 0 :(得分:2)

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

你不能返回unique_ptr。从函数返回值意味着使用复制构造函数。对unique_ptr禁用了复制构造函数。为什么?因为你不能复制unique_ptr,它是独一无二的!如果许多指针指向特定对象,则需要使用shared_ptr。赋值运算符(=)也是如此。

PS。我认为不推荐将对象内存地址显式地分配给unique_ptr(使用new),新标准强制您使用std :: make_unique以便为unique_ptr分配内容。 另外,首先尝试谷歌搜索你的错误,你会惊讶地发现有多少答案