2016-02-23 36 views
1

我下面的Apache節儉教程:阿帕奇節儉和CPP代碼生成

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 { 
... } 

這不是編譯由於事實上,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; 

於是問題來了:這是節儉的CPP代碼生成一個錯誤,它無法正確識別所需的基類或我錯在做什麼?

(這個問題是不是,因爲這是全部由儉生成的代碼固定C++編譯錯誤。這個問題是關於節儉)

+0

這看起來非常像(尚未固定的)[THRIFT-1372](https://issues.apache.org/jira/browse/THRIFT-1372)給我。 – JensG

+0

很難說沒有看到所有的代碼,但看起來雖然getStruct是在'SharedServiceHandler'中定義的,你實際上是從'SharedServiceIf'繼承的,它是一個純虛擬的(即一個接口)。所以看起來你可能需要實現它。道歉,如果這不現場,但很難說沒有所有的代碼。 – systemcpro

回答

1

是,骨骼與服務的繼承問題。如果您將該代碼與the code in the /tutorial/cpp folder of the source tree進行比較,您會看到一些顯着的差異。

我猶豫了一下,建議不要使用骨架代碼,但這可能是大多數人真正做的。他們使用源代碼樹教程和測試套件代碼作爲參考。事實上,C++是唯一可以生成骨架的目標語言。我認爲這一事實說明了很多。