【问题标题】:Apache thrift and cpp code generationApache thrift 和 cpp 代码生成
【发布时间】:2016-02-23 09:13:36
【问题描述】:

我正在关注 apache thrift 的教程:

https://svn.apache.org/repos/asf/thrift/attic/branches/0.9.x/tutorial/tutorial.thrift

它使用以下 shared.thrift:

https://svn.apache.org/repos/asf/thrift/attic/branches/0.9.x/tutorial/shared.thrift

我通过以下方式生成所需的 cpp 源文件:

thrift --gen cpp shared.thrift
thrift --gen cpp tutorial.thrift

它给了我一个 cpp 文件列表,在其中一个文件中我看到以下内容:

class CalculatorHandler : virtual public CalculatorIf {
  ... }

在哪里

class CalculatorIf : virtual public  ::shared::SharedServiceIf {
... }

 class SharedServiceIf {
 public:
  virtual ~SharedServiceIf() {}
  virtual void getStruct(SharedStruct& _return, const int32_t key) = 0;
};

由于virtual void getStruct是类中的纯虚,所以没有编译,但它定义在:

class SharedServiceHandler : virtual public SharedServiceIf {
  void getStruct(SharedStruct& _return, const int32_t key) {
   // Your implementation goes here
   printf("getStruct\n");
  }
}

这是编译错误:

Calculator_server.skeleton.cpp:49:63: error: cannot allocate an object of abstract type 'CalculatorHandler'
   shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
                                                               ^
Calculator_server.skeleton.cpp:19:7: note:   because the following virtual functions are pure within 'CalculatorHandler':
 class CalculatorHandler : virtual public CalculatorIf {
       ^
In file included from Calculator.h:12:0,
                 from Calculator_server.skeleton.cpp:4:
SharedService.h:18:16: note:    virtual void shared::SharedServiceIf::getStruct(shared::SharedStruct&, int32_t)
   virtual void getStruct(SharedStruct& _return, const int32_t key) = 0;

那么问题来了:这是 thrift 的 CPP 代码生成器中的一个错误,它无法正确识别所需的基类还是我做错了什么?

(这个问题不是关于修复 C++ 编译错误,因为这是所有由 thrift 生成的代码。这个问题是关于 thrift 的)

【问题讨论】:

  • 很难说没有看到所有代码,但似乎虽然 getStruct 是在SharedServiceHandler 中定义的,但您实际上是从SharedServiceIf 继承的,这是一个纯虚拟(即接口)。所以看来你可能必须实施它。抱歉,如果这没有到位,但如果没有所有代码就很难分辨。
  • 在我看来,这很像(仍然未修复的)THRIFT-1372

标签: c++ thrift


【解决方案1】:

是的,骨架在服务继承方面存在问题。如果将该代码与the code in the /tutorial/cpp folder of the source tree 进行比较,您会发现一些显着差异。

我有点犹豫是否建议不要使用骨架代码,但这可能是大多数人真正做的事情。他们使用源代码树教程和测试套件代码作为参考。事实上,C++ 是唯一生成骨架的目标语言。我认为这一事实本身就说明了很多。

【讨论】:

    猜你喜欢
    • 2012-04-14
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多