首页 > 解决方案 > 试图 push_back 一个向量进入私有向量没有返回函数的同一个类

问题描述

我目前正在研究强化学习问题,我正在尝试添加元素的位置vector<State>vector<Action>另一个浮点向量的位置哦继承类,这样我就可以操纵状态和动作的值。返回在 main 中的另一个函数上,所以我不能使用它。我正在尝试使用这个-> 但不工作。

渐变.h

class Gradient: virtual public Sarsa
{
public:
    Gradient();
    Gradient(float w);

    //states with weights
    std::vector<float> StateWeights(float s, float l, float a);

    //from the actions vector will be created with weights 
    std::vector<float> ActionWeights(Action a);
    void IsStateActionSeen();

private:

    float w0,w1,w2,w3,w4,w5,w6;

    std::vector<float> state_action_weights;
    std::vector<float> StatesPos;
    std::vector<float> Actions;
};

渐变.cpp

//returns the possition and the values of state in floationg points
std::vector<float> Gradient::StateWeights(float s, float l, float a)
{   
    std::vector<float> StateW;
    //position of the state in the vector
    int x = Sarsa::StateisNow ( s,  l,  a);//here i take the values of the main to find which state is not
    float y = (float)(x);
    std::cout<<"State found: "<<x<<std::endl;
    //read the values of state from the position
    State st = all_states.at(x);
    std::cout<<"The State is: "<<st.get_pos()<<" "<<st.get_ang()<<" "<<st.get_spe()<<std::endl;
    //create a vector with the values of the state and the weights
    StateW={y, st.get_pos(),st.get_ang(),st.get_spe() };
    std::cout<<"The State is: "<<StateW.at(0)<<" "<<StateW.at(1)<<" "<<StateW.at(2)<<" "<<StateW.at(3)<<std::endl;
    this->StatesPos.push_back(&StateW);//i want to add it in the vector before it returns
    return StateW;//return to the main
}

错误:没有匹配函数调用 'std::vector::push_back(std::vector*)' this->StatesPos.push_back(&StateW);

标签: c++11

解决方案


StatePos是浮点数的向量,您可以 push_back 仅浮点数(或将被隐式转换为浮点数的对象。

如果您需要附加两个向量,您可以执行以下操作:

StatePos.insert( StatePos.end(), StateW.begin(), StateW.end() ); 

推荐阅读