【发布时间】: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。