C++ FlatBuffers - 断言失败:(完成),函数完成

C++ FlatBuffers - Assertion failed: (finished), function Finished

这是我的架构:

namespace Vibranium;

enum GameObject_Type:uint { STATIC = 0 }

table GameObjectDatabase
{
    gameobjects:[GameObjectsTemplate];
}

table GameObjectsTemplate
{
    id:int;
    name:string;
    prefab:string;
    type:GameObject_Type;
    position_x:float;
    position_y:float;
    position_z:float;
    rotation_x:float;
    rotation_y:float;
    rotation_z:float;
    map_id:int;
    event_id:int;
}

root_type GameObjectDatabase;

这是我的代码:

flatbuffers::FlatBufferBuilder fbb;

std::vector<flatbuffers::Offset<Vibranium::GameObjectsTemplate>> gameobjects_template;

Field* fields = result->Fetch();
auto id = fields[0].GetInt32();
auto name = fbb.CreateString(fields[1].GetString());
auto prefab = fbb.CreateString(fields[2].GetString());
auto type = static_cast<Vibranium::GameObject_Type>(fields[3].GetInt32());
auto position_x = fields[5].GetFloat();
auto position_y = fields[6].GetFloat();
auto position_z = fields[7].GetFloat();
auto rotation_x = fields[8].GetFloat();
auto rotation_y = fields[9].GetFloat();
auto rotation_z = fields[10].GetFloat();
auto map_id = fields[11].GetInt32();
auto event_id = fields[12].GetInt32();

auto go1 = Vibranium::CreateGameObjectsTemplate(fbb, id, name, prefab, type,position_x,position_y,position_z, rotation_x,rotation_y,rotation_z,map_id,event_id);
gameobjects_template.push_back(go1);
this->_container.push_back(go1);
auto go_db = fbb.CreateVector(gameobjects_template);

Vibranium::GameObjectDatabaseBuilder go_builder(fbb);
go_builder.add_gameobjects(go_db);
go_builder.Finish();

auto gt = Vibranium::GetGameObjectDatabase(fbb.GetBufferPointer());

我收到这个错误:

Assertion failed: (finished), function Finished, file /opt/homebrew/include/flatbuffers/flatbuffers.h, line 1226.

是这一行造成的:

auto gt = Vibranium::GetGameObjectDatabase(fbb.GetBufferPointer());

为什么会这样,我该如何解决?

如果您得到断言,检查断言代码总是好的,它可能会提示您原因:

  void Finished() const {
    // If you get this assert, you're attempting to get access a buffer
    // which hasn't been finished yet. Be sure to call
    // FlatBufferBuilder::Finish with your root table.
    // If you really need to access an unfinished buffer, call
    // GetCurrentBufferPointer instead.
    FLATBUFFERS_ASSERT(finished);
  }