BAR
BAR

Reputation: 17121

How to build a struct in root struct for FlatBuffers C++

Seems like the flatbuffers authors missed an example for the Equipment struct in Monster and that would help here.

I have a struct (Message) within a root struct (MyProjectRootMessage). This seems to be the way to do it from the monster.fbs example.

enum MyProjectMsgType : byte { Message1=0, Message2 }

union Message { Message1, Message2 }

table MyProjectRootMessage {
    msg_type:MyProjectMsgType;
    message:Message;
}

table Message1 {
}

table Message2 {
}

But I'm trying to construct it in C++ and am getting an error for the message types. It says message is of type Offset<Message1> when it should be of type Message.

flatbuffers::FlatBufferBuilder builder;

auto message = Message1Builder(builder).Finish();

auto rootMessage = CreateMyProjectRootMessage(builder, MyProjectMsgType_Message1, message); // complains on message here

Upvotes: 0

Views: 314

Answers (1)

Aardappel
Aardappel

Reputation: 6074

You don't need enum MyProjectMsgType, the union already generates a type enum for you.

Offset<Message1> has a method that converts it into a generic offset suitable for use with unions.

Upvotes: 1

Related Questions