proto c++ 实现-“标记为‘覆盖’,但不覆盖”错误

jul*_*lka 11 c++ protocol-buffers grpc

// api_internal.proto
service InvoiceTemplateMatcher {
   rpc Process(InvoiceFilePath) returns (UploadStatus) {}
}

message InvoiceFilePath {
   string invoice_id = 1;
   string file_path = 2;
}

// template_matcher/src/main.cc
class OrkaEngineInvoiceTemplateMatcherImpl final : public InvoiceTemplateMatcher::Service {
private:
    Status Process(
        ServerContext* context,
        orka_engine_internal::InvoiceFilePath* invoicefp,
        orka_engine_internal::UploadStatus* response) override {
    // do stuff
    }
};
Run Code Online (Sandbox Code Playgroud)

InvoiceTemplateMatcher::Service是在编译期间从该.proto文件生成的。

当我尝试编译时,出现错误

‘grpc::Status OrkaEngineInvoiceTemplateMatcherImpl::Process(grpc::ServerContext*, orka_engine_internal::InvoiceFilePath*, orka_engine_internal::UploadStatus*)’ marked ‘override’, but does not override
     Status Process(ServerContext* context, orka_engine_internal::InvoiceFilePath* invoicefp, orka_engine_internal::UploadStatus* response) override {
Run Code Online (Sandbox Code Playgroud)

据我所知,我的代码与Route Guide example 中的编写方式相同。我错过了什么?

P.W*_*P.W 12

当函数未virtual在基类中标记时,编译器会发出此类错误。

考虑以下最小示例:

class Base{
    void Foo() {}
};

class Derived : Base{
    void Foo() override {}
};
Run Code Online (Sandbox Code Playgroud)

编译器发出错误:

error: 'void Derived::Foo()' marked 'override', but does not override
     void Foo() override {}
Run Code Online (Sandbox Code Playgroud)

演示

override符指定一个virtual功能覆盖其他virtual功能。

  • 只是为了记录此错误的另一个原因,我的函数在基类中被标记为虚拟,但派生类中该函数的**签名**与基类中的不同。这并不容易理解,因为派生类中有许多 typedef。 (3认同)

aik*_*khs 5

我知道这篇文章已经很老了,但我会为人们在使用 protobuf 时可能遇到的任何未来故障提供正确的答案。

你说得对,类实现是自动生成的,protobuf c++ 生成默认有这个类函数:

virtual ::grpc::Status Process(::grpc::ServerContext* context, const ::orka_engine_internal::InvoiceFilePath* request, ::orka_engine_internal::UploadStatus* response);
Run Code Online (Sandbox Code Playgroud)

所以你需要将你的函数与虚函数完全匹配。在你的例子中,只需更改invoicefprequest