0

I am new to CAP'N PROTO. I have created cap'n proto a structure and part of structure mentioned below:

**struct InjectorRequestMsg {
dataFrame @0: InjectorDataFrame;  
injectorRequestID @1: UInt32;
injectorID @2: UInt32;
injectorScriptPath @3: Text;
injectorFuncName @4: Text;
injectorLogPath @5: Text;

}**

Then, I am writing a builder in my C++ code as follows:
::capnp::MallocMessageBuilder message; InjectorRequestMsg::Builder injectorRequestMsg = message.initRoot<InjectorRequestMsg>();

Then, subsequent allocations also working fine and I am able to set all the values. But, when I try to declare and initialize InjectorRequestMsg::Builder like
InjectorRequestMsg::Builder injectorRequestMsg; injectorRequestMsg = message.initRoot<InjectorRequestMsg>();

Then, it gives below error: capnproto/InjectorMessage.capnp.h:197:3: error: declared here Builder() = delete; // Deleted to discourage incorrect usage.

Hence, could you please help me to understand how can I declare and initialize InjectorRequestMsg::Builder injectorRequestMsg in two different steps, so that I will make InjectorRequestMsg::Builder injectorRequestMsg as data-member of a C++ class and all member functions of C++ class can access it?

Biswojit
  • 1
  • 3
  • From the message you simply can't do it in two steps. I *think* you are asking about class members, you don't need `injectorRequestMsg = message.initRoot();` in a constructor, look at [member initialisation lists](https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list) – Caleth Jan 15 '19 at 16:27

1 Answers1

0

You can do:

InjectorRequestMsg::Builder injectorRequestMsg = nullptr;

The Cap'n Proto library often forces you to explicitly initialize to nullptr as a way to say: "I'm not ready to initialize this yet." It especially wants this for types that will crash the process if used uninitialized, to make it hard to screw up. In contrast, Reader types don't need to be explicitly initialized, because the default constructor can initialize the Reader to represent the struct's default value.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105