c++ - 如何在 Protocol Buffer 中设置嵌套消息的字段?

标签 c++ protocol-buffers

我有一个如下所示的原型(prototype)文件:

message terminal_data
{
    required int32 type = 1;              //1-->trade 2-->order

    message trade_data
    {
        optional string client_id =   1;
        optional string strat_id =    2;
        optional string symbol_name = 3;
        optional int64  trade_id =    4;
        optional string expiry =      5;
        optional int64  quantity =    6;
        optional string time =        7;

    }

    message order_data
    {
        optional string client_id =    1;
        optional string strat_id =     2;
        optional string symbol_name =  3;
        optional int64  order_id =     4;
        optional string side =         5;
        optional int64  quantity =     6;
        optional string time =         7;
    }

}

现在要设置数据,我这样做:

 tData.trade_data.mutable_client_id();

它提示:

:

 error: invalid use of 'data_model::terminal_data::trade_data'
     tData.trade_data.mutable_client_id();
           ^

设置嵌套消息的正确方法是什么?

这是生成的代码:

class terminal_data : public ::google::protobuf::Message {
 public:
  terminal_data();
  virtual ~terminal_data();

  terminal_data(const terminal_data& from);

  inline terminal_data& operator=(const terminal_data& from) {
    CopyFrom(from);
    return *this;
  }

  inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
    return _unknown_fields_;
  }

  inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
    return &_unknown_fields_;
  }

  static const ::google::protobuf::Descriptor* descriptor();
  static const terminal_data& default_instance();

  void Swap(terminal_data* other);

  // implements Message ----------------------------------------------

  terminal_data* New() const;
  void CopyFrom(const ::google::protobuf::Message& from);
  void MergeFrom(const ::google::protobuf::Message& from);
  void CopyFrom(const terminal_data& from);
  void MergeFrom(const terminal_data& from);
  void Clear();
  bool IsInitialized() const;

  int ByteSize() const;
  bool MergePartialFromCodedStream(
      ::google::protobuf::io::CodedInputStream* input);
  void SerializeWithCachedSizes(
      ::google::protobuf::io::CodedOutputStream* output) const;
  ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
  int GetCachedSize() const { return _cached_size_; }
  private:
  void SharedCtor();
  void SharedDtor();
  void SetCachedSize(int size) const;
  public:

  ::google::protobuf::Metadata GetMetadata() const;

  // nested types ----------------------------------------------------

  typedef terminal_data_trade_data trade_data;
  typedef terminal_data_order_data order_data;

  // accessors -------------------------------------------------------

  // required int32 type = 1;
  inline bool has_type() const;
  inline void clear_type();
  static const int kTypeFieldNumber = 1;
  inline ::google::protobuf::int32 type() const;
  inline void set_type(::google::protobuf::int32 value);

  // @@protoc_insertion_point(class_scope:data_model.terminal_data)
 private:
  inline void set_has_type();
  inline void clear_has_type();

  ::google::protobuf::UnknownFieldSet _unknown_fields_;

  ::google::protobuf::int32 type_;

  mutable int _cached_size_;
  ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];

  friend void  protobuf_AddDesc_data_5fmodel_2eproto();
  friend void protobuf_AssignDesc_data_5fmodel_2eproto();
  friend void protobuf_ShutdownFile_data_5fmodel_2eproto();

  void InitAsDefaultInstance();
  static terminal_data* default_instance_;
};

最佳答案

terminal_data 的原型(prototype)定义定义了一个嵌套的消息 trade_data - 但它实际上并没有定义一个类型为该消息的字段。 terminal_data 只有一个字段,type。添加类似的内容

optional trade_data trade = 2;
optional order_data order = 3;

和C++完全一样,你可以这样写

class Outer {
  class Inner {};
};

仅仅因为您已经定义了一个嵌套类,并不意味着外部类神奇地获得了一个类型为该嵌套类的成员。

关于c++ - 如何在 Protocol Buffer 中设置嵌套消息的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338372/

相关文章:

c++ - 如何正确扩展 std::thread 以包装生成的线程

c++ - 如何在 Visual Studio 2010 中混合使用 Fortran 和 C++?

c++ - 继承和内联?

java - 如何直接从protobuf创建GRPC客户端而不将其编译成java代码

c++ - Windows 上使用 C++ 的 google protobuf 时间戳未声明的标识符

C++ select 停止接受连接

c++ - dllimport 从 VC++ 到 VB 函数的转换,由于缺少链接库,dll 返回错误

javascript - 写入/读取 Protocol Buffer

java - 如何使用 protobuf-java-format 保留未知字段

java - Java中没有类名的 Protocol Buffer 反序列化