Apache thrift 和 cpp 代码生成
Apache thrift and cpp code generation
我正在学习 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 中是一个纯虚拟,但它是在
中定义的
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 代码生成器中的错误,它无法正确识别所需的基数 class 还是我做错了什么?
(这个问题不是关于修复 C++ 编译错误,因为这是所有由 thrift 生成的代码。这个问题是关于 thrift 的)
是的,骨架的服务继承有问题。如果将该代码与 the code in the /tutorial/cpp
folder of the source tree 进行比较,您会发现一些显着差异。
我有点犹豫是否建议完全不要使用框架代码,但这可能是大多数人真正做的。他们使用源代码树教程和测试套件代码作为参考。事实上,C++ 是唯一生成骨架的目标语言。我认为这个事实本身就说明了很多。
我正在学习 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 中是一个纯虚拟,但它是在
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 代码生成器中的错误,它无法正确识别所需的基数 class 还是我做错了什么?
(这个问题不是关于修复 C++ 编译错误,因为这是所有由 thrift 生成的代码。这个问题是关于 thrift 的)
是的,骨架的服务继承有问题。如果将该代码与 the code in the /tutorial/cpp
folder of the source tree 进行比较,您会发现一些显着差异。
我有点犹豫是否建议完全不要使用框架代码,但这可能是大多数人真正做的。他们使用源代码树教程和测试套件代码作为参考。事实上,C++ 是唯一生成骨架的目标语言。我认为这个事实本身就说明了很多。