我正在遵循有关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++编译错误,这个问题是而不是,因为这是所有通过节俭生成的代码。这个问题是关于节俭的)
最佳答案
是的,框架在服务继承方面存在问题。如果将该代码与the code in the /tutorial/cpp
folder of the source tree进行比较,将会看到一些明显的不同。
我有些犹豫,建议不要使用任何框架代码,但这可能是大多数人真正要做的。他们使用源代码树教程和Test Suite代码作为引用。实际上,C++是唯一生成骨架的唯一目标语言。我认为仅凭这一事实就可以说明很多问题。
关于c++ - Apache Thrift和CPP代码生成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35573286/