error flatbuffers create a vector of struct

Viewed 238

... this is my sample code:

struct QfeelSLAMNode {
    int mnNodeID;
    float mX,mY,mTheta;
    bool mbIsLandmark;
};

std::vector<Nodes>m_nodes;
std::vector<const Qflat::slam_node*> f_node;

    for (auto it = m_nodes.cbegin();it != m_nodes.cend();it++){
        Qflat::slam_node  node(it->mnNodeID,it->mX,it->mY,it->mTheta,it->mbIsLandmark);
        f_node.push_back(&node);
    }
    auto test_flat = builder.CreateVectorOfStructs(f_node.begin().base(),f_node.size());

    auto table = Qflat::CreateFbVTDataNodes(builder,node_data_type,testv2);

but when I create table ,I get this error:

No viable conversion from 'Offset<Vector<const Qflat::slam_node *const *>>' to 'Offset<Vector<const Qflat::slam_node *>>'

I don't why it happen. Any help on this issue would be greatly appreciated

1 Answers

First, you're adding a pointer to a local variable that is about to go out of scope to a vector in push_back. That's a dangling pointer. Don't ever do that in any C++ code, whether using FlatBuffers or not.

Second, the error already indicates the problem: the function expects a value type and you're supplying a pointer. So remove the * and the & from the code above.

Related